【问题标题】:EXC_BAD_ACCESS error using ALAssetsLibrary assetForURL:resultBlock:failureBlock:使用 ALAssetsLibraryassetForURL:resultBlock:failureBlock 的 EXC_BAD_ACCESS 错误:
【发布时间】:2011-05-16 14:55:10
【问题描述】:

我正在尝试从用户选择的图像中读取 EXIF 数据。我为此使用了 ALAssetLibrary。到目前为止,我已经设法获得了 assetForURL:resultBlock:failureBlock: 方法所需的参考 URL,但是当我尝试对参考 URL 执行任何操作时,我得到了 EXC_BAD_ACCESS 错误。

URL 的NSLog 在使用它之前会产生(据我所知是正确的)字符串:

assets-library://asset/asset.JPG?id=1000000003&ext=JPG

我一直在努力解决这个问题,但我似乎每次都陷入了死胡同。我必须承认我是 Objective-C 的新手,所以请随意批评我的代码。

代码(远非完整的类,但我认为应该足够了):

//Class_X.m

-(void)readExifDataFromSelectedImage:(NSURL *)imageRefURL    
{
    void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *) = ^(ALAsset *asset)
    {
       NSLog(@"Test:Succes");
    };

    ALAssetsLibrary *myAssetLib;
    NSLog(@"%@",imageRefURL);
    [myAssetLib assetForURL:imageRefURL
                resultBlock:ALAssetsLibraryAssetForURLResultBlock 
               failureBlock:^(NSError *error){NSLog(@"test:Fail");}];
}

//Class_Y.m
//This  also conforms to the UIImagePickerControllerDelegate And the NavigationControllerDelegate protocols:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.referenceURL = [info valueForKey:@"UIImagePickerControllerReferenceURL"];
    NSString *mediaType = [info
                       objectForKey:UIImagePickerControllerMediaType];
    [self dismissModalViewControllerAnimated:YES];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        imageView.image = selectedImage;
        btnNoPicture.hidden = YES;
        btnSelectPicture.hidden = YES;
        btnTakePicture.hidden = YES;
        imageView.hidden = NO;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Use this image?" 
                                                        message:@"Are you sure you want to use this image?" 
                                                       delegate:self 
                                              cancelButtonTitle:@"No" 
                                              otherButtonTitles:@"Yes", nil];
        [alert show];
        [alert release];
    }

}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        //Do not use the selected image.
        imageView.image = nil;
        imageView.hidden = YES;
        //Restart picking process
    }
    else
    {

        // I have an instance variable of type Class_X which i use 
        // throughout this class; let's call this variable "report". 
        // I also have the referenceURL stored as an instance variable.
        [self.report readExifDataFromSelectedImage:self.referenceURL];
    }

}

【问题讨论】:

    标签: objective-c cocoa-touch ios memory-management alassetslibrary


    【解决方案1】:

    EXC_BAD_ACCESS 通常是过度释放对象(悬空指针)的结果。由于库是异步操作的,因此您的块在 readExifDataFromSelectedImage: 方法返回后执行,因此此时 imageRefURL 可能已经被释放。在请求资产之前尝试retain URL,并在成功和失败块中尝试release

    【讨论】:

    • 干杯,这清除了我最初的错误。但是,这在调用 readExifFromSelectedImage 方法时是否会产生相同的 (EXC_BAD_ACCES) 错误(无论我如何解析 url,甚至硬编码 [NSURL URLWithString:@"assets-library://asset/asset.JPG?id=1000000003&ext =JPG"] 对此有什么建议吗?[self.report] 对象很好,不同的方法可以正常工作而不会出错。提前致谢:)
    • 嗯,看起来 myAssetLib 是未定义的。你声明了变量,但没有给它赋值……
    • 难以置信我怎么忽略了这一点。非常感谢!现在完美运行!
    猜你喜欢
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 2012-02-26
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多