开发者

EXC_BAD_ACCESS error using ALAssetsLibrary assetForURL:resultBlock:failureBlock:

I'm trying to read the EXIF data from an image, selected by a user. I'm using the ALAssetLibrary for this. So far I've managed to get the reference URL needed for the assetForURL:resultBlock:failureBlock: method, but when I try to do anything with the reference URL i get a EXC_BAD_ACCESS error.

An NSLog of the URL, right before using it, results in the (correct, as far as i know) string:

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

I've been trying to figure this out, but I seem to be hitting a dead end each time. I must admit I'm new to Objective-C in general, so please feel free to criticize my code accordingly.

Code (far from the complete classes, but i think it should be sufficient):

//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" 
开发者_如何学JAVA                                              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];
    }

}


EXC_BAD_ACCESS is most often the result of an over-released object (dangling pointer). As the library operates asynchronously, your block is executed after the readExifDataFromSelectedImage: method has returned, so imageRefURL is probably already deallocated at this point. Try to retain the URL before requesting the asset and release it in the success and failure blocks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜