iOS [UIImage imageWithContentsOfFile:filePath] EXC_BAD_ACCES



下面是代码。它下载缩略图,然后尝试基于缩略图文件路径创建图像。但它在方法调用"imageWithContentsOfFile"时给了我EXC_BAD_ACCESS错误。虽然EXC_BAD_ACCESS处理了试图访问已发布对象的代码,但我很可能不知道它可能是哪个对象。如果有任何帮助,我们将不胜感激!

NSBlockOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
            if([[NSFileManager defaultManager] fileExistsAtPath:operation.destinationPath ]){
               NSString *key = [[MEURLCacheKeyRegister sharedRegister] cacheKeyForURL:operation.fileUrl];
               UIImage *image = [UIImage imageWithContentsOfFile:operation.destinationPath];
            }else{
                DDLogDebug(@"Thumbnail file doesn't exist at %@", operation.destinationPath);
            }
        }
    }];
AFDownloadRequestOperation *requestOperation = [FileServerDownloadUtils downloadOperationForURL:operation.fileUrl
                                                                                      destinationPath:operation.destinationPath
                                                                                           completion:completionOperation];
[self.fileSyncQueue addOperation:requestOperation];

EXC_BAD_ACCESS表示对象在被访问时已被释放。

如果我是你,我会尝试以下事情:

  1. 使用.jpg而不是.jpg.prv.jpg扩展名保存文件
  2. 尝试使用initWithContentsOfFile而不是imageWithContentsOfFile作为imageWithContentsOfFile自动释放图像,这在极少数边缘情况下会造成这样的崩溃
  3. 将代码传递给block时,通过对self进行弱引用来访问对象属性。类似这样的东西:__weak MyController *weakSelf = self。然后使用weakSelf访问块内的属性

这些只是一些线索,可以帮助你进一步挖掘。您可以使用NSZombie和其他分析工具来确定它。

最新更新