【问题标题】:iPhone capture image crashes app when saving to Photos Album保存到相册时 iPhone 捕获图像崩溃应用程序
【发布时间】:2010-11-05 08:46:17
【问题描述】:

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

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

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

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

谢谢!

【问题讨论】:

  • 你得到 BAD_ACCESS 错误,还是什么?
  • 很遗憾我无法在实际设备上进行测试,我只能从客户那里得到反馈。 UIGetScreenImage() 函数在模拟器中也不可用。

标签: iphone uiimage capture


【解决方案1】:

由于这个问题太老了,我想展示更现代的方式来做你想做的事。
此代码可用于截屏:

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

【讨论】:

    【解决方案2】:

    你能检查一下权限吗?如果禁用,下次不会显示任何警报。

    【讨论】:

      猜你喜欢
      • 2011-07-26
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      相关资源
      最近更新 更多