开发者

iPhone capture image crashes app when saving to Photos Album

In my app i make a screen 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;

Then i want to save it to photos album:

UIImageWriteToSavedPhotosAlbum(anImage, self, @selector(save开发者_JS百科dPhotoImage: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];
}

The app crashes when saving to photos album. The captured image is ok, i can successfully upload and display it. I also tried loading an image from memory and saving it to Photos Album and it also worked.

I guess that i'm doing something wrong when i'm processing my image.. Any ideas?

Thanks!


As this question is too old I would like to show more modern way to do what you wanted.
This code can be used to capture the screen:

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

And this code to save the image:

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


Can you check the permission ? if its disabled, there is no alert will display next time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜