2017-02-05 61 views
1

我遇到一個奇怪的現象:導入圖像(SWIFT)

的imagePicker返回PHAsset,我做以下和管理數據與圖像呈現的ImageView:

asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions()) { (input, _) in 
    let url = input?.fullSizeImageURL 

    let imgV = UIImageView() 

    let test_url = URL(string: (url?.absoluteString)!) 

    print("><><><^^^><><>\(test_url)") 
    //prints: ><><><^^^><><>Optional(file:///var/mobile/Media/DCIM/107APPLE/IMG_7242.JPG) 
    let data = NSData(contentsOf: test_url! as URL) 
    imgV.image = UIImage(data: data! as Data) 
    imgV.backgroundColor = UIColor.cyan 

    att.imageLocalURL = url?.absoluteString//// saving the string to use in the other class 

    imgV.frame = CGRect(x: 0, y: 0, width: 100, height: 100) 

    self.view.addSubview(imgV) /// just to test that the file exists and can produce an image 

然而,當我在做的另一大類如下:

if((NSURL(string: self.attachment.imageLocalURL!) as URL!).isFileURL)// checking if is Local URL 
    { 


     let test_url = URL(string: self.attachment.imageLocalURL!) // reading the value stored from before 
     print("><><><^^^><><>\(test_url)") 
     //prints :><><><^^^><><>Optional(file:///var/mobile/Media/DCIM/107APPLE/IMG_7242.JPG) 
     let data = NSData(contentsOf: test_url! as URL) 
     imageView.image = UIImage(data: data! as Data) 

    } 

的數據是零!我做錯了什麼,URL的字符串在兩種情況下都是相同的!

+0

您是否嘗試過使用斷點,看一步一步,它產生了意想不到的結果? – d00dle

+0

A類:url_a - > string_a - > data - >圖像,但B類:string_a - > data - > nil,這是意想不到的行爲。 –

回答

0

PHAsset應通過PHImageManager類訪問對象。如果你想同步加載圖像,我建議你做這樣的事情:

func getImage(assetUrl: URL) -> UIImage? { 
     let asset = PHAsset.fetchAssets(withALAssetURLs: [assetUrl], options: nil) 

     guard let result = asset.firstObject else { 
      return nil 
     } 
     var assetImage: UIImage? 
     let options = PHImageRequestOptions() 
     options.isSynchronous = true 
     PHImageManager.default().requestImage(for: result, targetSize: UIScreen.main.bounds.size, contentMode: PHImageContentMode.aspectFill, options: options) { image, info in 
      assetImage = image 
     } 

     return assetImage 
    } 

你甚至可以寫一個UIImageView擴展到直接從PHAsset URL加載圖像。

0

VAR圖片:NSMutableArray的= NSMutableArray的()//保存獲取圖像

func fetchPhotos() 
{ 
    images = NSMutableArray() 
    //totalImageCountNeeded = 3 
    self.fetchPhotoAtIndexFromEnd(0) 
} 

func fetchPhotoAtIndexFromEnd(index:Int) 
{ 
    let status : PHAuthorizationStatus = PHPhotoLibrary.authorizationStatus() 
    if status == PHAuthorizationStatus.Authorized 
    { 
    let imgManager = PHImageManager.defaultManager() 
    let requestOptions = PHImageRequestOptions() 
    requestOptions.synchronous = true 

    let fetchOptions = PHFetchOptions() 
    fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: true)] 

    if let fetchResult: PHFetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions) 
    { 
     if fetchResult.count > 0 
     { 
       imgManager.requestImageForAsset(fetchResult.objectAtIndex(fetchResult.count - 1 - index) as! PHAsset, targetSize: CGSizeMake(self.img_CollectionL.frame.size.height/3, self.img_CollectionL.frame.size.width/3), contentMode: PHImageContentMode.AspectFill, options: requestOptions, resultHandler: { (image, _) in 
       //self.images.addObject(image!) 
       if image != nil 
       { 
        self.images.addObject(image!) 
       } 
       if index + 1 < fetchResult.count && self.images.count < 20 //self.totalImageCountNeeded 
       { 
        self.fetchPhotoAtIndexFromEnd(index + 1) 
       } 
       else 
       { 
       } 
      }) 
      self.img_CollectionL.reloadData() 

     } 
    } 
    } 
}