从UIImagePickerController中获取UIImage的URL

7

我正在尝试获取从图书馆导入的图像的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中无法正确实现。我错在哪里了?


错误信息会告诉你该怎么做,并且在stackoverflow上有很多例子。 - Eric Aya
@EricD。我尝试在代码中将值更改为NSURL,但在测试应用程序时无效。 - Francesco
3个回答

14

苹果在最新版本(iOS 9)中更改了其NSStringNSURL库中的某些内容,但这些方法从iOS 4开始就可用。您可以查看相关的Apple论坛帖子以获取更多详细信息。

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

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:返回URL路径中的最后一个组成部分。
  2. URLByAppendingPathComponent::通过追加一个路径组件创建一个新的URL。

@zumzum:我已经更新了我的Swift 3答案。感谢您的评论。 - Midhun MP
@MayankJain:你有检查过Swift 3版本吗?你能告诉我你遇到的问题是什么吗? - Midhun MP
Swift 3版本返回nil用于UIImagePickerControllerReferenceURL。 - PoolHallJunkie
@UmaAchanta,您能否请展示一下您遇到错误的代码?如果您正在使用相同的代码,我认为问题可能出在代码库的其他部分(例如缺少括号之类的)。 - Midhun MP
谢谢,@Midhun - 它已经编译通过了。在运行时发现的问题可以参考以下链接:https://dev59.com/H1kS5IYBdhLWcg3wu4vL - Uma Achanta

5

iOS 11版本后,你可以通过键UIImagePickerControllerImageURL从信息字典中获取图像的URL。

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

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

请不要将相同的答案复制粘贴到多个问题中。至少,您应该解释一下这如何解决问题以及为什么它不仅仅是一个通用的复制/粘贴。 - Andy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接