【问题标题】:Swift 3.0 Getting URL of UIImage selected from UIImagePickerControllerSwift 3.0 获取从 UIImagePickerController 中选择的 UIImage 的 URL
【发布时间】:2016-10-28 08:06:39
【问题描述】:

注意:- 此问题仅适用于 Swift 3.0 我能够获得通往 Swift 3.0 的优先路径

我想在 didFinishPickingMediaWithInfo 方法中获取 UIImage 的路径

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

但是这个路径并没有指向我的图像,即使 Documents 文件夹中没有任何图像。

谁能帮我解决这个问题?

【问题讨论】:

  • metode 被调用了吗?记录变量,尤其是imageUrl
  • @MayankJain 当图像根本没有存储在 DocumentDirectory 中时,它如何指向图像?现在告诉你想要实现什么。

标签: ios swift swift3 filepath nsurl


【解决方案1】:

您无法直接访问拾取图像的路径。您需要将其保存在 DocumentsDirectory 中,然后使用路径取回图像。

这样做

Swift 3.x

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    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!)

    if !FileManager.default.fileExists(atPath: localPath!.path) {
        do {
            try UIImageJPEGRepresentation(image, 1.0)?.write(to: localPath!)
            print("file saved")
        }catch {
            print("error saving file")
        }
    }
    else {
        print("file already exists")
    }
}

另请注意,您使用的名称作为每个文件的最后一个路径组件。因此,这只会将您的图像保存在 DocumentDirectory 中一次,因为它会在下一次找到路径。

现在,当您访问 localPath 变量并导航到路径时,您将找到图像。

注意:
如果您在此处使用设备,则需要下载设备的容器,显示其包内容并导航到文档目录,您将在其中找到保存的图像。

【讨论】:

    【解决方案2】:

    SWIFT 4 你可以试试这个,它工作正常。

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    
    
        if let imgUrl = info[UIImagePickerControllerImageURL] as? URL{
            let imgName = imgUrl.lastPathComponent
            let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
            let localPath = documentDirectory?.appending(imgName)
    
            let image = info[UIImagePickerControllerOriginalImage] as! UIImage
            let data = UIImagePNGRepresentation(image)! as NSData
            data.write(toFile: localPath!, atomically: true)
            //let imageData = NSData(contentsOfFile: localPath!)!
            let photoURL = URL.init(fileURLWithPath: localPath!)//NSURL(fileURLWithPath: localPath!)
            print(photoURL)
    
        }
    
        APPDEL.window?.rootViewController?.dismiss(animated: true, completion: nil)
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-31
      • 2016-01-10
      • 2016-10-21
      • 2016-05-18
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多