无法从资产网址加载图片

Cod*_*ian 6 iphone alassetslibrary

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

我的班级已ResourceURLString定义为此类

@property NSURL *ResourceURL;
Run Code Online (Sandbox Code Playgroud)

我正在设置属性,同时循环assets通过电话

Item.ResourceURLString = [[asset valueForProperty:ALAssetPropertyURLs] objectForKey:[[asset valueForProperty:ALAssetPropertyRepresentations] objectAtIndex:0]];
Run Code Online (Sandbox Code Playgroud)

当用户点击图像时,我想加载图像.

我的代码就是这个

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

Img = [UIImage imageWithData:imageUrl];
Run Code Online (Sandbox Code Playgroud)

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

Mid*_* MP 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];
Run Code Online (Sandbox Code Playgroud)


mwf*_*ire 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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

从UIImagePickerController委托获取imageReferenceURL:

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

设置图像

let imageView = UIImageView()
imageView.imageFromAssetURL(imageURL)
Run Code Online (Sandbox Code Playgroud)

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


小智 6

iOS 8开始,你可以使用 Photos Framework,这里是如何在Swift 3中完成的

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
    }
}
Run Code Online (Sandbox Code Playgroud)

如果要检索图片的原始大小,请使用PHImageManagerMaximumSize.但是,如果你想获取一个较小的或特定的大小可以替换PHImageManagerMaximumSize通过CGSize(width:150, height:150)