2015-01-31 39 views
6

我試圖提供用戶從照片庫中選擇圖像。因此我使用UIImagePickerController進行選擇。但是,在獲取文件系統中的初始URL時出現了一個問題(我需要這個用於CKAsset)。獲取從UIImagePickerController中選擇的UIImage的URL

我的代碼。

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 
    let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL 
    let path = imageURL.path! 
    let imageName = path.lastPathComponent 
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 
    let documentDirectory = paths.first as String! 
    let localPath = documentDirectory + "/" + imageName 

    let imageData = NSData(contentsOfFile: localPath)! 
    let image = UIImage(data: imageData)! 

    picker.dismissViewControllerAnimated(true, completion: nil) 

} 

IMAGEURL是有點神祕。它描述資產URL,但不是文件系統中的一個。它看起來像:assets-library://asset/asset.JPG?id=B6C0A21C-07C3-493D-8B44-3BA4C9981C25&ext=JPG

根據此邏輯,路徑/asset.JPG其中asset.JPG是圖像的名稱。

然後我訪問我的文檔文件夾,並試圖找到那裏與路徑的文件:

/用戶/尤金/庫/開發商/ CoreSimulator /設備/ 3C5E9A23-8220-4B37-BD14,F1E42EEC2C7C /data/Containers/Data/Application/20EBAAE2-6C6F-4651-A48F-16A222CCB3A2/Documents/asset.JPG

隱祕的很好,但似乎是一個不存在圖像的真實路徑......沒有圖片可以找到該路徑。這讓我感到噁心。

我是否必須從頭開始保存圖像?或者還有什麼其他的路要走嗎?

我已經瀏覽了幾篇教程,使用AssetsLibrary API,但我沒有找到任何有用的東西來解決我的問題。先謝謝你!

回答

24

好的,我已經解決了這個問題。

您只需抓取圖像(info[UIImagePickerControllerOriginalImage] as UIImage)並保存到指定目錄即可。如果你只需要保存1張圖片,效果很好。下面的代碼ID。

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 
    let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL 
    let imageName = imageURL.path!.lastPathComponent 
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String 
    let localPath = documentDirectory.stringByAppendingPathComponent(imageName) 

    let image = info[UIImagePickerControllerOriginalImage] as UIImage 
    let data = UIImagePNGRepresentation(image) 
    data.writeToFile(localPath, atomically: true) 

    let imageData = NSData(contentsOfFile: localPath)! 
    let photoURL = NSURL(fileURLWithPath: localPath) 
    let imageWithData = UIImage(data: imageData)! 

    picker.dismissViewControllerAnimated(true, completion: nil) 

} 
+3

你能否更新你的答案Swift 2?謝謝! – 2015-11-04 15:53:36

+5

不能在Swift 3中工作,我轉換了代碼以使其編譯,但返回的路徑並未指向Swift 3的選定圖像 – zumzum 2016-10-14 19:53:54

+0

,請檢查此解決方案:http://stackoverflow.com/questions/4957972/iphone-uiimagepickercontroller - 保存圖像到應用文件夾/ 43456006#43456006 – XueYu 2017-04-24 01:16:02

3
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 
//this block of code grabs the path of the file 
let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL 
let imagePath = imageURL.path! 
let localPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(imagePath) 

//this block of code adds data to the above path 
let path = localPath.relativePath! 
let imageName = info[UIImagePickerControllerOriginalImage] as UIImage 
let data = UIImagePNGRepresentation(imageName) 
data?.writeToFile(imagePath, atomically: true) 

//this block grabs the NSURL so you can use it in CKASSET 
let photoURL = NSURL(fileURLWithPath: path) 
} 
+0

@Peter Kreinz,希望這一個幫助 – DHuang 2016-02-21 00:54:10

1

檢查這個解決方案。

import AssetsLibrary 

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
      self.imagePicker = picker 
      if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 
      let controller = CropViewController(image: selectedImage) 
      controller.delegate = self 
      picker.presentViewController(controller, animated: true, completion: nil) 
      } 

     else if let imageUrl = info[UIImagePickerControllerReferenceURL] as? NSURL{ 
      let assetLibrary = ALAssetsLibrary() 
      assetLibrary.assetForURL(imageUrl , resultBlock: { (asset: ALAsset!) -> Void in 
      if let actualAsset = asset as ALAsset? { 
       let assetRep: ALAssetRepresentation = actualAsset.defaultRepresentation() 
       let iref = assetRep.fullResolutionImage().takeUnretainedValue() 
       let image = UIImage(CGImage: iref) 
       let controller = CropViewController(image: image) 
       controller.delegate = self 
       picker.presentViewController(controller, animated: true, completion: nil) 
      } 
      }, failureBlock: { (error) -> Void in 
      }) 
     } 
     } 
0

SWIFT 4ü可以試試這個,它的正常工作。

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


    if let imgUrl = info[UIImagePickerControllerImageURL] as? URL{ 
     let imgName = imgUrl.lastPathComponent 
     let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first 
     let localPath = documentDirectory?.appending(imgName) 

     let image = info[UIImagePickerControllerOriginalImage] as! UIImage 
     let data = UIImagePNGRepresentation(image)! as NSData 
     data.write(toFile: localPath!, atomically: true) 
     //let imageData = NSData(contentsOfFile: localPath!)! 
     let photoURL = URL.init(fileURLWithPath: localPath!)//NSURL(fileURLWithPath: localPath!) 
     print(photoURL) 

    } 

    APPDEL.window?.rootViewController?.dismiss(animated: true, completion: nil) 
} 
相關問題