iphone - 无法从 Assets URL 加载图像

标签 iphone alassetslibrary

我有一个类,用于存储有关手机上的 Assets (图像、视频)的信息。

我的类有这样定义的ResourceURLString

@property NSURL *ResourceURL;

我在循环手机上的 Assets 时设置属性

Item.ResourceURLString = [[asset valueForProperty:ALAssetPropertyURLs] objectForKey:[[asset valueForProperty:ALAssetPropertyRepresentations] objectAtIndex:0]];

当用户单击图像时,我想加载该图像。

我的代码是这样的

NSData *imageUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[CurrentItem.ResourceURL absoluteString]]];    

Img = [UIImage imageWithData:imageUrl];

但图像始终为零 我已验证 ResourceURL 属性包含 URL Assets :library://asset/asset.JPG?id=82690321-91C1-4650-8348-F3FD93D14613&ext=JPG

最佳答案

您无法以这种方式加载图像。

您需要为此使用 ALAssetsLibrary 类。

将 assetlibrary 框架添加到您的项目并添加头文件。

使用以下代码加载图像:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
    ALAssetRepresentation *rep = [myasset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    if (iref) {
        UIImage *largeimage = [UIImage imageWithCGImage:iref];
        yourImageView.image = largeImage;
    }
};

ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
{
    NSLog(@"Can't get image - %@",[myerror localizedDescription]);
};

NSURL *asseturl = [NSURL URLWithString:yourURL];
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:asseturl 
                   resultBlock:resultblock
                  failureBlock:failureblock];

关于iphone - 无法从 Assets URL 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14496910/

相关文章:

ios - SWIFT ALAssetsLibrary 不枚举组

ios - 找出我的异步调用何时完成

ios - IPA 创建失败,运行 build-ios-bundle

iphone - 如何从非标准 URL NSString 对象中提取参数?

iphone - 启动 iPhone 应用程序时的 EXC_BREAKPOINT

iphone - UINavigationBar 调用 drawRect : if subclassed but not when using categories in iOS 5?

iphone - 使用 ALAssetsLibrary 访问单个图像

iphone - 获取照片 Assets 库 Assets 表示大小为零

ios - 如何使用 fetchAssetsWithALAssetURLs 获取 PHAsset

iphone - 当我缩小时,MKMapView 会自动放大到当前位置