Getting UIImage URLs from UIImagePickerController

I'm trying to get the URL of an image imported from a library in Swift to send it to Apple Watch using transferFile(_:metadata) , but I have two errors 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 get an error with * imageName and * localPath because it says that:

'lastPathComponent' is not available: use lastPathComponent in NSURL instead. 'stringByAppendingPathComponent' is not available: use the URLByAppendingPathComponent in NSURL instead.

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

+3
source share
3 answers

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

To fix this error you need to change the code as follows:

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); } 

Link:

+11
source
 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
source

Starting with iOS 11, you can retrieve the URL of an image from an information dictionary using the UIImagePickerControllerImageURL key.

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let imageURL = info[UIImagePickerControllerImageURL] as? URL } 
0
source

Source: https://habr.com/ru/post/1258537/


All Articles