3

I have some images in my Photo Album. I have already retrieved the asset urls of all of them.. I want to get a particular image using the asset url... below code gave me asset url..

    ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];

                      NSString *uti = [defaultRepresentation UTI];
                      NSURL *URL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];

Now using this url, how can I get the image(UIImage)?

codersnet
  • 135
  • 1
  • 3
  • 11
  • http://stackoverflow.com/questions/3837115/display-image-from-url-retrieved-from-alasset-in-iphone and http://stackoverflow.com/questions/14496910/unable-to-load-image-from-asset-url – iPatel Feb 01 '14 at 05:14

2 Answers2

10

Get Url by this,

NSURL *url= (NSURL*) [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]];

And get image from url like below.

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
    ALAssetRepresentation *rep = [myasset defaultRepresentation];
    @autoreleasepool {
        CGImageRef iref = [rep fullScreenImage];
        if (iref) {
            UIImage *image = [UIImage imageWithCGImage:iref];
            self.loadedImage = image;
            dispatch_async(dispatch_get_main_queue(), ^{
                //UIMethod trigger...
            });
            iref = nil;
        }
    } 
};

ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
{
    NSLog(@"Can't get image - %@",[myerror localizedDescription]);
};

ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:yourUrl
               resultBlock:resultblock
              failureBlock:failureblock];
Mani
  • 17,226
  • 13
  • 73
  • 97
  • 1
    @Jamil If we do the JOB while scrolling the table view means, it may slow down the rendering for the case sync. that's why I'm using async. – Mani Nov 10 '16 at 09:50
0
UIImage *img = [UIImage imageWithCGImage:[[myAsset defaultRepresentation] fullResolutionImage]

Hope this helps

iPatel
  • 41,165
  • 13
  • 109
  • 131
Sunny Shah
  • 12,687
  • 8
  • 45
  • 79