gpt4 book ai didi

objective-c - EXC_BAD_ACCESS 错误使用 ALAssetsLibrary assetForURL :resultBlock:failureBlock:

转载 作者:行者123 更新时间:2023-11-28 20:46:21 25 4
gpt4 key购买 nike

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

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

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

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

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

//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];
}

}

最佳答案

EXC_BAD_ACCESS 通常是过度释放对象(悬挂指针)的结果。由于库异步运行,您的 block 在 readExifDataFromSelectedImage: 方法返回后执行,因此此时 imageRefURL 可能已被释放。在请求 Assets 之前尝试保留 URL,并在成功和失败 block 中释放它。

关于objective-c - EXC_BAD_ACCESS 错误使用 ALAssetsLibrary assetForURL :resultBlock:failureBlock:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6019214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com