开发者

UIImagePicker and ALAAssetRepresentation problem in iOS5

I've noticed that the NSDictionary returned by UIImagePicker for Camera Roll seems to no longer return a UIImagePickerControllerOriginalImage object. Instead I get a UIImageControllerReferenceURL, requiring me to use the ALAAssetLibrary's assetForURL to create a UIImage through a long and involved process.

There are enough examples of this on the web, all remarkably similar, which I've implemented, but after creating the ALARepresentation object, calling any of its methods causes a EXC_BAD_ACCESS exception, so I'm unable to retrieve the image. Does anyone know a way to get back an "original image" rather than a "reference url", or has this behaviour been silently deprecated?

NOTE: I'm working under iOS 5 (because I like the memory management) and Xcode 4.2 for the storyboard feature. Going back to earlier versions seems not possible. The code looks like this:

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

The EXC_BAD_ACCESS happens at the line trying to create the CGImageRef. I've tried the other *rep methods (including metadata); these also throw the exception. The only thing that "works" is getting a thumbnail CGImageRef from the asset, but I need the full resolution image.

Finally, even if it worked, the asynchronous nature (using callback blocks) is not really what I want as it complicates the picker process no end in my case. Is there any other way to pick an image from the camera roll?


i found this somewhere on the web a while back, it works on ios5 and is along the same lines as what you want to do, hope it helps

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

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜