无法从资产网址加载图片

时间:2013-01-24 08:31:20

标签: iphone alassetslibrary

我有一个课程,用于存储有关手机资产的信息(图像,视频)。

我的班级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];

但是Image总是为零 我已验证ResourceURL属性包含URL 资产:library://asset/asset.JPG?id=82690321-91C1-4650-8348-F3FD93D14613&ext=JPG

5 个答案:

答案 0 :(得分:14)

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

您需要使用ALAssetsLibrary类。

将assetslibrary框架添加到项目中并添加头文件。

使用以下代码加载图片:

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];

答案 1 :(得分:6)

从iOS 9.0开始,ALAssetsLibrary已弃用。从iOS 8.0开始,这适用于PHPhotoLibrary。这是一个小的UIImage扩展,Swift 2X。 这使用固定的图像大小。

import Photos

extension UIImageView {

    func imageFromAssetURL(assetURL: NSURL) {

        let asset = PHAsset.fetchAssetsWithALAssetURLs([assetURL], options: nil)

        guard let result = asset.firstObject where result is PHAsset else {
           return
        }

        let imageManager = PHImageManager.defaultManager()

        imageManager.requestImageForAsset(result as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: PHImageContentMode.AspectFill, options: nil) { (image, dict) -> Void in
            if let image = image {
                self.image = image
            }
        }
    }
}

从UIImagePickerController委托获取imageReferenceURL:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    imageURL = info[UIImagePickerControllerReferenceURL] as? NSURL
}

设置图像

let imageView = UIImageView()
imageView.imageFromAssetURL(imageURL)

可能还有我还没有遇到的影响,经典是UITableViewCell或线程问题。我会保持更新,也感谢您的反馈。

答案 2 :(得分:5)

由于 iOS 8 ,您可以使用Photos Framework这里是如何在 Swift 3

中执行此操作
Dynamic t MyB

如果要检索图片的原始大小,请使用 PHImageManagerMaximumSize 。但是,如果您想要检索较小或特定的尺寸,可以将{strong> PHImageManagerMaximumSize 替换为import Photos // use the Photos Framework // declare your asset url let assetUrl = URL(string: "assets-library://asset/asset.JPG?id=9F983DBA-EC35-42B8-8773-B597CF782EDD&ext=JPG")! // retrieve the list of matching results for your asset url let fetchResult = PHAsset.fetchAssets(withALAssetURLs: [assetUrl], options: nil) if let photo = fetchResult.firstObject { // retrieve the image for the first result PHImageManager.default().requestImage(for: photo, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFill, options: nil) { image, info in let myImage = image //here is the image } }

答案 3 :(得分:0)

ALAsset *asset = "asset array index"
[tileView.tileImageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];

答案 4 :(得分:0)

对于 Swift 5

fetchAssets(withALAssetURLs) 将在未来版本中删除。因此我们使用 fetchAssets 从资产本地标识符获取图像

extension UIImageView {
func imageFromLocalIdentifier(localIdentifier: String, targetSize: CGSize) {
        let fetchOptions = PHFetchOptions()
        // sort by date desending
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        // fetch photo with localIdentifier
        let results = PHAsset.fetchAssets(withLocalIdentifiers: [localIdentifier], options: fetchOptions)
        let manager = PHImageManager.default()
        results.enumerateObjects { (thisAsset, _, _) in
            manager.requestImage(for: thisAsset, targetSize: targetSize, contentMode: .aspectFit, options: nil, resultHandler: {(image, _) in
                DispatchQueue.main.async {[weak self] in
                    self?.image = image
                }
            })
        }
    }
}

更新

let image = UIImage(data: NSData(contentsOf: imageURL as URL)! as Data)