【问题标题】:Saving photo to library more than once using ALAssetsLibrary causes crash使用 ALAssetsLibrary 将照片多次保存到库会导致崩溃
【发布时间】:2014-07-24 18:42:32
【问题描述】:

我有一种将照片保存到用户照片库的方法。此代码在您第一次将照片保存到图库时运行良好,但如果您再次尝试保存,应用程序将崩溃。

方法如下:

- (void)saveToLibray
{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    // Request to save the image to camera roll
    [library writeImageToSavedPhotosAlbum:[self.image CGImage] orientation:(ALAssetOrientation)[self.image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){

        if (error) {

            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"Could not save photo to your library. Please enable Halfsies in Settings > Privacy > Photos." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];

            CGImageRelease([self.image CGImage]);

        } else if (!error) {

            UIAlertView *librarySaveSuccessAlertView = [[UIAlertView alloc]initWithTitle:@"Success!" message:@"Your finished halfsie was successfully saved to your photo library!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

            [librarySaveSuccessAlertView show];

         CGImageRelease([self.image CGImage]);

        }
    }];
}

我已确定释放CGImage,我也在等待librarySaveSuccessAlertView 显示在屏幕上,单击它的确定按钮(cancelButtonTitle),然后我再次尝试保存。

第二次应用总是崩溃。

这是因为图书馆可以检测到重复的照片,还是其他原因?

编辑:崩溃详情

在应用程序崩溃后以绿色突出显示的行上显示:

Thread 1: EXC_BAD_ACCESS(code=1, address=0x657471ef)

编辑 2:好的,所以我现在意识到这很可能是因为我正在尝试向已发布的对象发送消息。我很困惑我应该如何处理这个问题。即使我释放了self.imageCGImage 数据,我应该仍然能够在第二次保存时再次访问它对吗?

【问题讨论】:

  • 崩溃是什么?你知道,这些东西通常信息量很大。不要只说“它崩溃了”。实际发生了什么?它可能与您刚刚显示的代码无关......
  • 能把崩溃的日志放上来
  • 抱歉,我没有太多的调试经验。请查看我的编辑。
  • 请看我的第二次编辑。
  • 你已经解决了这个问题,但是,僵尸是一个很好的技术。

标签: ios alassetslibrary


【解决方案1】:

正如您已经怀疑的那样(做得好),问题出在这一行:

CGImageRelease([self.image CGImage])

当且仅当您调用 CreateCopy 方法来获取它时,您才需要释放 CG 类型的对象。例如,您可能通过调用函数CGImageCreate() 获得了这个CGImage。你在这里没有做类似的事情,所以这不是你要发布的对象。所以只要剪掉这些线,停止不正确的内存管理,一切都会好起来的。

【讨论】:

  • 现在运行良好。感谢您的帮助。我将阅读您的文章并进一步探索僵尸。谢谢。
  • 你做了所有的工作!我没有仔细阅读你的代码,因为你没有说崩溃是什么。一旦你说出它是什么并且弄清楚这意味着什么,问题就很明显了。你是那里的 9/10。很好的侦探工作。
猜你喜欢
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 2017-01-31
  • 1970-01-01
  • 1970-01-01
  • 2017-02-06
  • 2011-07-26
相关资源
最近更新 更多