【问题标题】:iOS - Bad Access when calling ALAssetsLibrary several timesiOS - 多次调用 ALAssetsLibrary 时访问错误
【发布时间】:2015-01-05 20:55:34
【问题描述】:

当用户从 iPhone 库上传图片时,我使用 ALAssetsLibrary 提取位置:

ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
            [assetslibrary assetForURL:urlPhoto
                           resultBlock:^(ALAsset *asset) {
                               CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation];
                           } failureBlock:^(NSError *error) {
                               NSLog(@"Can not get asset - %@",[error localizedDescription]);
                           }];

但是,如果用户上传一张图片,然后返回上传屏幕并上传另一张图片,则在上传三到四次后,应用在运行assetForURL:resultBlock:failureBlock: 方法时会在EXC_BAD_ACCESS 上崩溃。

我猜这是因为 assetForURL:resultBlock:failureBlock: 异步运行而 ALAssetsLibrary 由于某种原因被释放,尽管它​​不能像我构建应用程序的方式同时运行。

如何防止它崩溃?

编辑

虽然崩溃总是发生在这一点上(由于较早的解除),但由于之前已解除分配的UITableView 而发生了错误,但此时引用了它的委托/数据源。 修复正在添加:

- (void) dealloc
{
    myTableView.dataSource = nil;
    myTableView.delegate = nil;
}

在具有 TableView 的 UIViewController 的末尾。

【问题讨论】:

  • 您在哪一行得到错误?在assetLibrary =?还是回调?
  • 你能试着把你的回调放在主队列上吗?
  • 我添加了日志来查看它在哪里崩溃,因为只在 main.c 中捕获。似乎它开始运行assetForURL:resultBlock:failureBlock: 方法,但从未到达回调方法。
  • 试图在主线程上做回调,改变在主线程上执行的结果:[assetslibrary assetForURL:urlPhoto resultBlock:^(ALAsset *asset) { [self performSelectorOnMainThread:@selector(checkLocation:) withObject:asset waitUntilDone:YES]; } failureBlock:^(NSError *error) { NSLog(@"Can not get asset - %@",[error localizedDescription]); }];仍然崩溃...

标签: ios iphone uiimagepickercontroller exc-bad-access alassetslibrary


【解决方案1】:

只需将您的对象更改为属性。

界面:

@property (nonatomic, strong) ALAssetsLibrary* assetslibrary;

比调用实现:

if (self.assetslibrary == nil) {    
    self.assetslibrary = [[ALAssetsLibrary alloc] init];
}
[self.assetslibrary assetForURL:urlPhoto
                           resultBlock:^(ALAsset *asset) {
                               CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation];
                           } failureBlock:^(NSError *error) {
                               NSLog(@"Can not get asset - %@",[error localizedDescription]);
                           }];

【讨论】:

  • 不幸的是,它仍然不起作用。根据 NSLog,我有 ALAssetsLibrary 的分配不会失败。在进入回调之前运行方法assetForURL时会发生这种情况。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多