保存到相册时,iPhone捕获图像会崩溃应用程序

时间:2010-11-05 08:46:18

标签: iphone uiimage capture

在我的应用程序中,我进行了一次屏幕截图:

 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];
}

保存到相册时应用程序崩溃了。 捕获的图像没问题,我可以成功上传并显示它。 我还尝试从内存加载图像并将其保存到Photos Album,它也有效。

我想我正在处理我的图像时做错了什么.. 有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

由于这个问题太老了,我想表现出更现代的方式来做你想做的事 此代码可用于捕获屏幕:

- (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);
    }
}];}

答案 1 :(得分:-1)

你能检查一下这个权限吗?如果禁用,下次没有警报显示。