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

在某个地方保存图像路径。然后,您可以使用:

恢复图像
func imageFromPath(path: String) -> UIImage? {
  if let data = NSData(contentsOfFile: path) {
    return UIImage(data: data)
  }
  return nil
}

相关内容

  • 没有找到相关文章

最新更新