【问题标题】:Get the Image (UIImage) using asset URL in iOS 7在 iOS 7 中使用资产 URL 获取图像(UIImage)
【发布时间】:2014-02-24 23:45:32
【问题描述】:

我的相册中有一些图像。我已经检索了所有这些资产的 url。我想使用资产 url 获取特定的图像……下面的代码给了我资产 url ..

    ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];

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

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

【问题讨论】:

标签: ios ios7 alassetlibrary


【解决方案1】:

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

【讨论】:

  • @Jamil 如果我们在滚动表格视图时执行 JOB,则可能会减慢案例同步的渲染速度。这就是我使用异步的原因。
【解决方案2】:
UIImage *img = [UIImage imageWithCGImage:[[myAsset defaultRepresentation] fullResolutionImage]

希望对你有帮助

【讨论】:

  • 这不使用资产 URL,因此不回答问题。
猜你喜欢
  • 2019-05-26
  • 1970-01-01
  • 2015-10-10
  • 2016-09-18
  • 2021-06-10
  • 2015-08-02
  • 1970-01-01
  • 2019-11-30
  • 1970-01-01
相关资源
最近更新 更多