2013-01-24 158 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)