【问题标题】:Getting URL of UIImage from UIImagePickerController从 UIImagePickerController 获取 UIImage 的 URL
【发布时间】:2016-01-10 04:26:34
【问题描述】:

我正在尝试获取从 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 中正确使用它。我哪里出错了?

【问题讨论】:

  • 错误信息告诉你该怎么做。 SO上有很多例子。
  • @EricD。我尝试在代码中将值更改为 NSURL,但这在测试应用程序时不起作用。

标签: ios objective-c swift nsurl


【解决方案1】:

从 iOS 11 开始,您可以使用 UIImagePickerControllerImageURL 键从信息字典中获取图像 URL。

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

【讨论】:

    【解决方案2】:

    Apple 在其最新版本 (iOS 9) 中更改了其 NSStringNSURL 库中的某些内容,但这些方法可从 iOS 4 中获得。您可以查看相关的 Apple Forum Post 了解更多详细信息。

    要修复此错误,您需要更改以下代码:

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

    【讨论】:

    • @zumzum:我已经更新了我对 Swift 3 的回答。感谢您的评论
    • @MayankJain:你检查过 Swift 3 版本吗?你能告诉我你面临的问题是什么吗?
    • Swift 3 版本为 UIImagePickerControllerReferenceURL 返回 nil
    • @UmaAchanta 你能告诉我你得到那个错误的代码吗?如果您使用相同的代码,我认为问题可能出在代码库的其他部分(例如缺少括号)
    • 谢谢,@Midhun - 已编译。在运行时发现的问题是stackoverflow.com/questions/39521667/…
    【解决方案3】:
    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)
    }
    

    【讨论】:

    • 请不要将相同的answer 复制并粘贴到多个问题中。至少,您应该解释一下这是如何解决 this 问题的,以及为什么它不仅仅是通用的复制/粘贴。
    猜你喜欢
    • 2015-03-31
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多