ios - 在 iOS 7 中使用 Assets URL 获取图像 (UIImage)

标签 ios ios7 alassetlibrary

我的相册中有一些图片。我已经检索了所有这些的 Assets 网址。我想使用 Assets 网址获取特定图像...下面的代码给了我 Assets 网址。

    ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];

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

现在使用这个 url,我怎样才能得到图像(UIImage)?

最佳答案

通过这个获取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];

关于ios - 在 iOS 7 中使用 Assets URL 获取图像 (UIImage),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21494338/

相关文章:

icons - 图标的大小和名称以及 Xcode 5、ios 7 中的启动图像

ios - sizeWithAttributes 给我一个不同的 CGSize 宽度和高度

ios - AssetLibrary - 如何加载方向校正?

ios - 标签栏 Controller 中的图像和标题与 iPhone X 重叠?

ios - 在 minimumFontSize 之后获取 UILabel 字体 pointsize

ios - Xcode 9.3 构建在调用 URL 方案时崩溃(深层链接)

ios - AirPrint 和一个 NSMutableString

iphone - Xcode 5 自动布局 : collection of views inside another view