iOS5中的UIImagePicker和ALAAssetRepresentation问题

时间:2011-07-27 22:50:42

标签: uiimagepickercontroller ios5 alassetslibrary

我注意到UIImagePicker for Camera Roll返回的NSDictionary似乎不再返回UIImagePickerControllerOriginalImage对象。相反,我得到一个UIImageControllerReferenceURL,要求我使用ALAAssetLibrary的assetForURL通过漫长而复杂的过程创建一个UIImage。

在网上有足够的这个例子,都非常相似,我已经实现了,但是在创建ALARepresentation对象之后,调用它的任何方法会导致EXC_BAD_ACCESS异常,所以我无法检索图像。有没有人知道一种方法来取回“原始图像”而不是“参考网址”,或者这种行为是否已被默默地弃用?

注意:我在 iOS 5 (因为我喜欢内存管理)和Xcode 4.2的故事板功能下工作。回到早期版本似乎是不可能的。代码如下所示:

// in the header
#import <AssetsLibrary/AssetsLibtrary.h>

typedef void (^ALAAssetsLibraryAssetForURLReferenceBlock)(ALAAsset *asset);
typedef void (^AlAAssetsLibraryAccessForFailureBlock)(NSError *error);


// in the implementation
-(void) ImagePickerController:(UIImagePickerController*)picker
               didFinishPickingMediaWithInfo:(NSDictionary*)info
{
  NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
  if (mediaType isEqualToString:(NSString*)kUTTypeImage]) {
    NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
    if (url) {
      ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) {
        ALAssetRepresentation *rep = [asset defaultRepresentation];
        CGImageRef iref = [rep fullResolutionImage];
        if (iref) {
           UIImage *image = [UIImage imageWithCGImage:iref];
           [self displayImage:image];
        }
      };
      ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *error) {
        // handle error
      };

      ALAssetsLibrary *assetsLib = [[ALAssetLibrary alloc]init];
      [assetsLib assetForURL:url resultBlock:resultblock failureBlock:failureblock];
    }
  }
}

EXC_BAD_ACCESS发生在尝试创建CGImageRef的行。我尝试过其他* rep方法(包括元数据);这些也抛出异常。唯一“有效”的是从资产中获取缩略图CGImageRef,但我需要全分辨率图像。

最后,即使它起作用,异步性质(使用回调块)也不是我想要的,因为它使得选择器进程变得复杂,在我的情况下没有结束。有没有其他方法可以从相机胶卷中选择图像?

1 个答案:

答案 0 :(得分:0)

我在网络上的某个地方发现这一段时间,它可以在ios5上运行,并且与您想要做的一致,希望有所帮助

   -(void)readExifDataFromSelectedImage:(NSURL *)imageRefURL    
{
    void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *) = ^(ALAsset *asset)
    {

    };

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




- (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];
        myUIImageView.image = selectedImage;

        myUIImageView.hidden = NO;


        [alert release];
    }

}