【问题标题】:Instantiation of ALAssetsLibrary inside block throws bad access块内 ALAssetsLibrary 的实例化会引发错误访问
【发布时间】:2012-04-03 08:25:16
【问题描述】:

我将我的项目转换为 arc,现在,当我实例化一个新的资产库时,它会引发错误的访问错误。在 ARC 之前没有问题。

有什么建议吗?

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:stillImageConnection
                                                   completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) 
{
    if (error) {
        NSLog(@"Take picture failed");
    }
    else 
    {
        NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
        CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, 
                                                                    imageDataSampleBuffer, 
                                                                    kCMAttachmentMode_ShouldPropagate);
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library writeImageDataToSavedPhotosAlbum:jpegData 
                                         metadata:(__bridge_transfer id)attachments
                                  completionBlock:^(NSURL *assetURL, NSError *error) {
                                      if (error) {
                                          NSLog(@"Save to camera roll failed");
                                      }
                                  }];

        if (attachments)
            CFRelease(attachments);
    }
}];

【问题讨论】:

  • 有时生活会如此艰难。在 ARC 之前,我不必保留 CFDictionaryRef。所以在初始化附件后添加CFRetain(attachments); 删除了错误的访问。干杯,
  • 请将您的解决方案放在答案而不是评论中:blog.stackoverflow.com/2011/07/…

标签: cocoa-touch avfoundation alassetslibrary image-capture


【解决方案1】:

请注意,在应用程序的整个生命周期中,您应该只初始化一次资产库。所以你应该例如在 appdelegate 或其他单例中执行此操作。您的代码似乎有风险帽子资产库被多次初始化。

干杯,

亨德里克

【讨论】:

    【解决方案2】:

    有时生活会如此艰难。在 ARC 之前,我不必保留 CFDictionaryRef。所以添加 CFRetain(attachments);初始化附件后,删除了错误的访问权限。

    干杯, 浴缸

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      相关资源
      最近更新 更多