抓取从UIImagePickerController保存的图像?

我正在尝试从文档路径加载图像,在我的示例中显示为file:///var/mobile/Containers/Data/Application/AE7B9E43-E92D-4029-AE8D-4500CB61C15D/Documents/uploadImage.jpg。我怎么才能把那张照片拿回来,这样就可以用电话发出去了?我正在保存imagePickerController中的图像。这是我保存图像的代码。。。

    let fileDirectory : NSURL  = {
        return try! FileManager.default.url(for: .documentDirectory , in: .userDomainMask , appropriateFor: nil, create: true)
    }() as NSURL

    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    let imagePath = fileDirectory.appendingPathComponent("uploadImage.jpg")

    guard let imageData = UIImageJPEGRepresentation(image, 0.5) else {
        // handle failed conversion
        presentAlert(title: "Error", message: "Image Failure")
        print("jpg error")
        return
    }

    try! imageData.write(to: imagePath!
        print("Image Path: \(imagePath!)")


最佳答案:

您可以使用

let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
  if let dirPath  = documentPath.first{
    let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("uploadImage.jpg")
    let image    = UIImage(contentsOfFile: imageURL.path)
    // Do whatever you want with the image
  }