簡體   English   中英

保存到相冊時,iPhone捕獲圖像使應用程序崩潰

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM