2017-03-27 107 views
1

我是iOS開發新手,無法弄清楚如何從本地目錄加載imageView中的圖像。 我使用imagePickerController來選取圖像,獲取其信息,然後使用這些信息在imageView中顯示圖像。Swift 3從本地directory下載圖片

下面是代碼挑圖像和信息詞典:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     let image    = info[UIImagePickerControllerOriginalImage] as! UIImage 

     let imageUrl   = info[UIImagePickerControllerReferenceURL] as! NSURL 
     let imageName   = imageUrl.lastPathComponent 
     let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! 
     let photoURL   = NSURL(fileURLWithPath: documentDirectory) 
     let localPath   = photoURL.appendingPathComponent(imageName!)! 

     imagesList.append(localPath) 

     picker.dismiss(animated: true, completion: nil) 
    } 

那時,這裏就是我在尋找一些幫助,我希望爲使用了localPath或IMAGEURL加載圖像在另一個圖像查看,但無法弄清楚如何。 我試過

func changeImage(_ sender: Any) { 
     if imagesList.count > 0 { 
      let imageUrlPath = imagesList[indexList] 
      let urlString: String = imageUrlPath.absoluteString 

      imageView.image = UIImage(contentsOfFile: urlString) 

      indexList = (indexList + 1) % imagesList.count 
     } 
    } 

但我不能有什麼工作。 任何想法如何我可以實現這一點?

謝謝你的幫助。

+0

您需要使用'imageUrlPath.path'而不是'imageUrlPath.absoluteString' –

+0

感謝您的幫助。我沒有任何錯誤,但圖像不顯示在imageView中。如果我打印urlString,它只包含「/asset.JPG」,我認爲這不夠。 什麼contentsOfFile應該包含:整個路徑(file:/// Users/me/Library/Developer/CoreSimulator/Devices/BF7D44E4-26DD-43EE-5543-D7AB7654F8DC/data/Containers/Data/Application/C9561694- 34E9-46AD-83AA-38F067557F86 /文檔/ asset.JPG)?或者像「assets-library://asset/asset.JPG?id = 9A443DCB-EC44-42B8-8773-B597CF782EDD&ext = JPG」 – radar

+0

打印路徑並檢查圖像是否存在 –

回答

2

您需要保存對PHAsset對象的引用,而不是URL字符串。

開始定義你的陣列爲:

var imagesList = [PHAsset]() 

然後,在didFinishPickingMediaWithInfo

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

    dismiss(animated: true) 

    // get the selected image as a UIImage object 
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage 
    imageView.image = image 

    // save a PHAsset reference in imagesList array for later use 
    if let imageUrl = info[UIImagePickerControllerReferenceURL] as? URL{ 

     let assets = PHAsset.fetchAssets(withALAssetURLs: [imageUrl], options: nil) 

     if let p = assets.firstObject { 

      imagesList.append(p) 

     } 

    } 

} 

接下來,添加一對夫婦的 「方便」 功能:

func getAssetFullsize(asset: PHAsset) -> UIImage { 
    let manager = PHImageManager.default() 
    let option = PHImageRequestOptions() 
    var img = UIImage() 
    option.isSynchronous = true 
    let w = asset.pixelWidth 
    let h = asset.pixelHeight 
    manager.requestImage(for: asset, targetSize: CGSize(width: w, height: h), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in 
     img = result! 
    }) 
    return img 
} 

func getAssetThumbnail(asset: PHAsset) -> UIImage { 
    let manager = PHImageManager.default() 
    let option = PHImageRequestOptions() 
    var thumbnail = UIImage() 
    option.isSynchronous = true 
    let w = 100 
    let h = 100 
    manager.requestImage(for: asset, targetSize: CGSize(width: w, height: h), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in 
     thumbnail = result! 
    }) 
    return thumbnail 
} 

最後,當你想得到實際的圖像:

func changeImage(_ sender: Any) { 
    if imagesList.count > 0 { 

     // get the full-size image from PHAsset 
     imageView.image = getAssetFullsize(asset: imagesList[indexList]) 

     // or, just get a thumbnail-sized image 
     //imageView.image = getAssetThumbnail(asset: imagesList[indexList]) 

     indexList = (indexList + 1) % imagesList.count 

    } 
} 

當然,你會想要添加適當的錯誤檢查,你自己的大小,命名,跟蹤等......但我認爲這是你想要的方向。

+0

非常感謝DonMag,我會在測試後再回答更長的時間。 – radar

+0

這正是我所期待的。非常感謝你。 – radar