在iOS 7中,如何使用资源URL获取图像(UIImage)

3

我在我的相册中有一些图片。我已经检索出了它们所有的资产URL。我想使用该资产URL获取特定的图片......以下代码为我提供了资产URL...

    ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];

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

现在使用这个url,我怎样才能获取图片(UIImage)?

请返回翻译后的文本。 https://dev59.com/sW865IYBdhLWcg3wZNrT 和 https://dev59.com/vm3Xa4cB1Zd3GeqPgZrh - iPatel
2个回答

10

通过这个获取URL:

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

可以像以下方式从 URL 中获取图像。

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

1
@Jamil 如果我们在滚动表视图时执行任务,可能会减慢同步渲染的速度。这就是为什么我使用异步的原因。 - Mani

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

希望这能有所帮助。

4
这并未使用资产网址,因此并没有回答问题。 - Michael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接