[英]iPhone capture image crashes app when saving to Photos Album
在我的应用程序中,我进行了屏幕截图:
UIImage *viewImage = [UIImage imageWithCGImage:UIGetScreenImage()];
CGRect cropRect = CGRectMake(0, 0, 320, 440);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], cropRect);
viewImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
captureImage = [[UIImage alloc] init];
captureImage = viewImage;
然后我想将其保存到相册:
UIImageWriteToSavedPhotosAlbum(anImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
- (void) savedPhotoImage:(UIImage *)image
didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
NSString *themessage = @"This image has been saved to your Photos album. Tap to continue.";
NSString *errorTitle = @"Image saved.";
if (error) {
themessage = [error localizedDescription];
errorTitle = @"error";
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:errorTitle
message:themessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
保存到相册时,应用程序崩溃。 捕获的图像还可以,我可以成功上传并显示它。 我还尝试了从内存中加载图像并将其保存到相册中,并且它也可以正常工作。
我想我在处理图像时做错了。有什么想法吗?
谢谢!
由于这个问题太旧了,我想展示更现代的方式来做您想要的事情。
此代码可用于捕获屏幕:
- (UIImage *) getScreenShot
{
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
并使用以下代码保存图像:
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^
{
PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:screenShot];
}
completionHandler:^(BOOL success, NSError *error)
{
if (success)
{
NSLog(@"successfully saved");
}
else
{
NSLog(@"error saving to photos: %@", error);
}
}];}
您可以检查许可吗? 如果已禁用,则下次不会显示任何警报。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.