小编典典

从UIImagePickerController获取UIImage的URL

swift

我试图获取从Swift中的Library导入的图像的URL,以将其发送到Apple
Watch,transferFile(_:metadata)但出现两个错误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中无法正确解决。


阅读 323

收藏
2020-07-07

共1个答案

小编典典

苹果改变的东西在他们NSStringNSURL图书馆在其最新版本(iOS版9),但这些方法都可以从iOS版4.您可以检查有关苹果论坛帖子的更多细节。

要修复此错误,您需要更改代码,例如:

斯威夫特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);
}

斯威夫特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:
2020-07-07