【发布时间】: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