繁体   English   中英

iOS-多次调用ALAssetsLibrary时访问错误

[英]iOS - Bad Access when calling ALAssetsLibrary several times

当用户从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的末尾。

只需将您的对象更改为property即可。

接口:

@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]);
                           }];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM