我有一个类,用于存储有关手机上的 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/