Display image from URL retrieved from ALAsset on iPhone

I am using the ALAsset Framework to access files in the device gallery.

So far, I can access the thumbnail and display it.
I want to display the actual image in the image view, but I cannot figure out how to do this.

I tried using the URL field in an ALAsset object, but was unsuccessful.

Does anyone know how to do this?

Here is some code where I managed to access the thumbnail and put it in a table cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } //here 'asset' represents the ALAsset object asset = [assets objectAtIndex:indexPath.row]; //i am accessing the thumbnail here [cell.imageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]]; [cell.textLabel setText:[NSString stringWithFormat:@"Photo %d", indexPath.row+1]]; return cell; } 
+66
url iphone image
01 Oct '10 at 7:02
source share
4 answers

The API has slightly changed the rules, and you no longer have direct access to the file system in the iPhoto library. Instead, you get the URL of the resource library.

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

You use the ALAssetLibrary object to access the ALAsset object through a URL.

so from the docs for ALAssetLibrary write this in the header (or your source)

 typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset); typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error); 

which is not strictly necessary, but keeps things pretty. and then at your source.

 -(void)findLargeImage { NSString *mediaurl = [self.node valueForKey:kVMMediaURL]; // ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) { ALAssetRepresentation *rep = [myasset defaultRepresentation]; CGImageRef iref = [rep fullResolutionImage]; if (iref) { largeimage = [UIImage imageWithCGImage:iref]; [largeimage retain]; } }; // ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) { NSLog(@"booya, cant get image - %@",[myerror localizedDescription]); }; if(mediaurl && [mediaurl length] && ![[mediaurl pathExtension] isEqualToString:AUDIO_EXTENSION]) { [largeimage release]; NSURL *asseturl = [NSURL URLWithString:mediaurl]; ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease]; [assetslibrary assetForURL:asseturl resultBlock:resultblock failureBlock:failureblock]; } } 

A few things to note that this uses blocks that were new to me before I started porting iOS4, but you can take a look

https://www.mikeash.com/pyblog/friday-qa-2008-12-26.html

and

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html

They tilt their heads slightly, but if you think of them as notification selectors or callbacks, that helps.

Also

  • when findLargeImage returns resultblock will not be launched as its callback. So largeImage will not be valid yet.
  • largeImage must be an instance variable not bound to a Method.

I use this construct to do this using this method, but you may find something more suitable for your use.

 [node.view findLargeImage]; UIImage *thumb = node.view.largeImage; if (thumb) { blah blah } 

What I learned while trying to get this to work anyway.

IOS 5 update

When the resulting block is triggered with iOS5 and possibly single-core devices, I cannot rely on the image available immediately after calling findLargeImage . So I changed it to call a delegate.

 @protocol HiresImageDelegate <NSObject> @optional -(void)hiresImageAvailable:(UIImage *)aimage; @end 

and comme cรก

 // ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) { ALAssetRepresentation *rep = [myasset defaultRepresentation]; CGImageRef iref = [rep fullResolutionImage]; if (iref) { UIImage *largeimage = [UIImage imageWithCGImage:iref]; [delegate hiresImageAvailable:large]; } }; 
+93
Oct 02 '10 at
source share

Warren answered well for me. One useful thing for some people is the simultaneous inclusion of image orientation and large-scale metadata. You do this in your results block as follows:

 ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) { ALAssetRepresentation *rep = [myasset defaultRepresentation]; CGImageRef iref = [rep fullResolutionImage]; if (iref) { UIImage *largeimage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:[rep orientation]]; [delegate hiresImageAvailable:large]; } }; 

In this case, the call to imageWIthCGImage has the scale and orientation added when creating the UIImage for you.

 [UIImage imageWithCGImage:iref scale:[rep scale] orientation:[rep orientation]]; 

One trick that should be noted is that if you use [rep fullScreenImage] instead of [rep fullResolutionImage] on iOS 5, you get an image that is already rotated - however, it is in the iPhone screen resolution, that is, it has lower resolution.

+15
Jan 23
source share

Just to combine the answers of Warren and oknox into a shorter fragment:

 ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; [assetsLibrary assetForURL:self.selectedPhotos[i] resultBlock: ^(ALAsset *asset){ ALAssetRepresentation *representation = [asset defaultRepresentation]; CGImageRef imageRef = [representation fullResolutionImage]; if (imageRef) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; imageView.image = [UIImage imageWithCGImage:imageRef scale:representation.scale orientation:representation.orientation]; // ... } } failureBlock: ^{ // Handle failure. }]; 

I personally like to set failureBlock to nil .

+9
Sep 02 '13 at 12:27
source share
  NSURL* aURL = [NSURL URLWithString:@"URL here"]; ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; [library assetForURL:aURL resultBlock:^(ALAsset *asset) { UIImage *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.5 orientation:UIImageOrientationUp]; cell.backgroundView = [[UIImageView alloc] initWithImage:copyOfOriginalImage]; } failureBlock:^(NSError *error) { // error handling NSLog(@"failure-----"); }]; 

just provide the UIReferenceURl that you got for the image in the photo library above ... it just works fine.,. I did it in

  • UIcollectionView Cell

    .. if you just want to display it in

  • UIImageView

    means

Edit

 cell.backgroundView = [[UIImageView alloc] initWithImage:copyOfOriginalImage]; 

For

 imageView.image = copyOfOriginalImage; 
+8
Sep 19 '13 at 7:45
source share



All Articles