屏幕截图未出现在“照片”应用中 (iOS obj-c)
Screenshot doesn't appear at Photos app (iOS obj-c)
我正在尝试使用下一个代码保存我的应用程序的屏幕截图:
UIView* view = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
NSURL* documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSString* filename = [NSString stringWithUTF8String: @"screenshot.png"];
NSString* path =[NSString stringWithFormat:@"%@/%@",[documentsDirectory path], filename];
BOOL saved = [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil];
NSLog(saved ? @"Saved" : @"Not saved");
它记录 Saved
,但图像没有出现在照片应用程序中。
documentsDirectory
: /var/mobile/Containers/Data/Application/003D7989-D88E-48FF-A3C5-86A340C6F077/Documents
我做错了什么?感谢回复。
尝试使用此代码在照片应用程序中保存图像
NSData * data = UIImagePNGRepresentation(image);
UIImageWriteToSavedPhotosAlbum(compressedPNGImage,
self,
@selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:),
NULL);
那么你应该按照下面的方式实现上面的选择器
- (void)thisImage:(UIImage *)image hasBeenSavedInPhotoAlbumWithError:(NSError *)error usingContextInfo:(void*)ctxInfo
{
if (error)
{
// not saved
}
else
{
// saved
}
}
最后,一定要在信息目标设置中添加"Privacy - Photo Library Usage Description"键
我正在尝试使用下一个代码保存我的应用程序的屏幕截图:
UIView* view = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
NSURL* documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSString* filename = [NSString stringWithUTF8String: @"screenshot.png"];
NSString* path =[NSString stringWithFormat:@"%@/%@",[documentsDirectory path], filename];
BOOL saved = [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil];
NSLog(saved ? @"Saved" : @"Not saved");
它记录 Saved
,但图像没有出现在照片应用程序中。
documentsDirectory
: /var/mobile/Containers/Data/Application/003D7989-D88E-48FF-A3C5-86A340C6F077/Documents
我做错了什么?感谢回复。
尝试使用此代码在照片应用程序中保存图像
NSData * data = UIImagePNGRepresentation(image);
UIImageWriteToSavedPhotosAlbum(compressedPNGImage,
self,
@selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:),
NULL);
那么你应该按照下面的方式实现上面的选择器
- (void)thisImage:(UIImage *)image hasBeenSavedInPhotoAlbumWithError:(NSError *)error usingContextInfo:(void*)ctxInfo
{
if (error)
{
// not saved
}
else
{
// saved
}
}
最后,一定要在信息目标设置中添加"Privacy - Photo Library Usage Description"键