问题描述
我有一个课程,用于存储有关手机资产的信息(图片,视频)。
I have a class that stores information about the assets on the phone (images, videos).
我的班级有 ResourceURLString
定义为
@property NSURL *ResourceURL;
我在通过资产在手机上这样
Item.ResourceURLString = [[asset valueForProperty:ALAssetPropertyURLs] objectForKey:[[asset valueForProperty:ALAssetPropertyRepresentations] objectAtIndex:0]];
当用户点击图片时,我想加载图片。
When the user clicks on an image I want to load the image.
我的代码是这个
NSData *imageUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[CurrentItem.ResourceURL absoluteString]]];
Img = [UIImage imageWithData:imageUrl];
但是Image总是nil
我已经验证了ResourceURL属性包含URL
资产: library://asset/asset.JPG?id = 82690321-91C1-4650-8348-F3FD93D14613& ext = JPG
But the Image is always nilI have verified that the ResourceURL property contains the URLassets:
library://asset/asset.JPG?id=82690321-91C1-4650-8348-F3FD93D14613&ext=JPG
推荐答案
您无法以这种方式加载图片。
You can't load images in this way.
您需要使用
ALAssetsLibrary
这个类。
将assetslibrary框架添加到项目中并添加头文件。
Add assetslibrary framework to your project and add header files.
使用以下代码加载图片:
Use the below code for loading image:
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];
这篇关于无法从资产网址加载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!