2013-01-24 157 views
3

我有一个类存储有关手机资源(图像,视频)的信息。无法从资产URL加载图像

我班有这样定义

@property NSURL *ResourceURL; 

我设置的属性,而在手机上循环槽的assets这样

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

当对图像的用户点击我想ResourceURLString加载图像。

,我所拥有的代码是这样

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

Img = [UIImage imageWithData:imageUrl]; 

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

+0

真正的问题不谈,你转换'NSURL'成'NSString'形式,然后再返回,这是完全没有意义的 –

回答

13

您无法加载图像这条路。

您需要为此使用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]; 
+0

我有跟着你的代码和如果我想添加图像URL到这行代码[_pinterest createPinWithImageURL:[NSURL URLWithString:@“??”];什么URLString我应该在那里得到一个图像在pinterest? – MayurCM

+0

@MayurCM:'yourURL'代表资产网址,它看起来像这样:'library://asset/asset.JPG?id = 82690321-91C1-4650-8348-F3FD93D14613&ext = JPG'。您无法在此处使用网址。资源库用于从光盘库中加载图像,而不是从网络或软件包中加载。 –

+0

但是当我传递assetsLibrary时,我也无法在pinterest上插图。 – MayurCM

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

作为iOS的9.0 ALAssetsLibrary被弃用。自iOS8.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的或线程问题。我会保持这个更新,也感谢您的反馈。

3

由于的iOS 8可以使用Photos Framework这里是如何做到这一点的斯威夫特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 
    } 
} 

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