从UIImagePickerController获取UIImage的URL

我试图从Swift库中导入的图像的URL用transferFile(_:metadata)发送给Apple Watch,但是我在NSURL上有两个错误。

这是我的代码:

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

我得到* imageName和* localPath的错误,因为它说:

'lastPathComponent'不可用:改为在NSURL上使用lastPathComponent。 'stringByAppendingPathComponent'不可用:改为在NSURL上使用URLByAppendingPathComponent。

但是我不能在Swift 2.0和Xcode 7中正确地使用它。我哪里错了?

苹果已经在他们的最新版本(iOS 9)的NSStringNSURL库中改变了一些东西,但这些方法可以从iOS 4中获得。你可以查看相关的Apple Forum Post以获取更多细节。

为了解决这个错误,你需要改变代码:

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

参考:

  1. lastPathComponent
  2. URLByAppendingPathComponent:
 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) }