7

I'm trying to get the URL of an image imported from Library in Swift to send it to Apple Watch with transferFile(_:metadata) but I'm having two error on NSURL.

This is my code:

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

    data!.writeToFile(localPath, atomically: true)

    let photoURL = NSURL(fileURLWithPath: localPath)

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

And I'm getting error with *imageName and *localPath because it says that:

'lastPathComponent' is unavailable: Use lastPathComponent on NSURL instead. 'stringByAppendingPathComponent' is unavailable: Use URLByAppendingPathComponent on NSURL instead.

But I can't get it right in Swift 2.0 and Xcode 7. Where am I going wrong?

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
Francesco
  • 169
  • 1
  • 1
  • 14

3 Answers3

14

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:
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • @zumzum: I've updated my answer for Swift 3. Thanks for your comment – Midhun MP Oct 15 '16 at 17:39
  • @MayankJain: Did you check the Swift 3 version ? Can you tell me what is the issue you are facing ? – Midhun MP Oct 28 '16 at 18:01
  • Swift 3 version return nil for UIImagePickerControllerReferenceURL – PoolHallJunkie Jan 19 '17 at 12:42
  • @UmaAchanta Can you please show me the code in which you are getting that error ? If you are using the same code, I think the issue might be with some other part of your code base (Something like missing brackets) – Midhun MP Jul 26 '17 at 12:44
  • thanks, @Midhun - Its compiled. the issue found in runtime is https://stackoverflow.com/questions/39521667/class-plbuildversion-is-implemented-in-both-applications – Uma Achanta Jul 26 '17 at 12:48
5

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
}
Allanah Fowler
  • 1,251
  • 16
  • 17
0
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)
}
DHuang
  • 93
  • 1
  • 8
  • Please don't copy and paste the same [answer](http://stackoverflow.com/a/35530982/189134) to multiple questions. At the very least, you should explain how this solves *this* question and why it isn't simply a generic copy/paste. – Andy Feb 21 '16 at 06:32