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

2019-02-19 04:30发布

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

2条回答
Animai°情兽
2楼-- · 2019-02-19 04:39
UIImage *img = [UIImage imageWithCGImage:[[myAsset defaultRepresentation] fullResolutionImage]

Hope this helps

查看更多
够拽才男人
3楼-- · 2019-02-19 05:00

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];
查看更多
登录 后发表回答