Setting a UIImageView with ALAsset

3.6k views Asked by At

I have a UIImageView and would like to set it to the contents of an ALAsset.

How is the ALAssets contents used to set a UIImage?

3

There are 3 answers

1
Aaron Brager On
ALAssetRepresentation *rep = [self defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:[rep fullScreenImage]];
0
pinxue On

As mentioned by Clayton and Aaron Brager, the key is imageWithCGImage method of UIImage class.

There are a two ways to get image content:

  • asset.thumbnail or asset.aspectThumbnail
  • via a presentation of the asset
    • there are default presentation and presentation for UTI
    • CGImage prepared
      • CGImageWithOptions, fullResolutionImage, fullScreenImage
    • raw data
      • getBytes:fromOffset:length:error:
      • (if you want to keep EXIF etc, use imageWithData instead of imageWithCGImage)

You may refer http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetRepresentation_Class/Reference/Reference.html#//apple_ref/doc/c_ref/ALAssetRepresentation for more details.

2
Clayton On

Try this. Asset is ALAsset by the way.

UIImage *image = [UIImage imageWithCGImage:[asset thumbnail] scale:1.0 orientation:[[asset defaultRepresentation] orientation]]

UIImageView *image_view = [[UIImageView alloc] initWithImage:image]