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

假装没事ソ 提交于 2019-12-01 05:51:58

问题


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


回答1:


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



回答2:


UIImage *img = [UIImage imageWithCGImage:[[myAsset defaultRepresentation] fullResolutionImage]

Hope this helps



来源:https://stackoverflow.com/questions/21494338/get-the-image-uiimage-using-asset-url-in-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!