iosios7alassetlibrary

Get the Image (UIImage) using asset URL in iOS 7


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)?


Solution

  • 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];