Getting URL of UIImage from UIImagePickerController

流过昼夜 提交于 2019-11-29 07:13:10

Apple has changed something in their NSString and NSURL library in their latest release (iOS 9), but those methods are available from iOS 4. You can check the related Apple Forum Post for more details.

For fixing this error, you need to change the code like:

Swift 2:

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!)
{
    let imageUrl          = editingInfo[UIImagePickerControllerReferenceURL] as! NSURL
    let imageName         = imageUrl.lastPathComponent
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String!
    let photoURL          = NSURL(fileURLWithPath: documentDirectory)
    let localPath         = photoURL.URLByAppendingPathComponent(imageName!)
    let image             = editingInfo[UIImagePickerControllerOriginalImage]as! UIImage
    let data              = UIImagePNGRepresentation(image)

    data!.writeToFile(localPath.absoluteString, atomically: true)

    self.dismissViewControllerAnimated(true, completion: nil);
}

Swift 3:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    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!)
    let image             = info[UIImagePickerControllerOriginalImage]as! UIImage
    let data              = UIImagePNGRepresentation(image)

    do
    {
        try data?.write(to: localPath!, options: Data.WritingOptions.atomic)
    }
    catch
    {
        // Catch exception here and act accordingly
    }

    self.dismiss(animated: true, completion: nil);
}

Reference:

  1. lastPathComponent
  2. URLByAppendingPathComponent:

As of iOS 11, you can get the image URL from the info dictionary with the key UIImagePickerControllerImageURL.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let imageURL = info[UIImagePickerControllerImageURL] as? URL
}
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)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!