【问题标题】:Grabbing Image Saved from UIImagePickerController?抓取从 UIImagePickerController 保存的图像?
【发布时间】:2017-03-08 02:10:07
【问题描述】:

我正在尝试从文档路径加载图像,在我的情况下显示为file:///var/mobile/Containers/Data/Application/AE7B9E43-E92D-4029-AE8D-4500CB61C15D/Documents/uploadImage.jpg。我将如何取回该图像以便可以使用 POST 调用将其发送出去?我正在从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!)")

【问题讨论】:

    标签: ios swift uiimagepickercontroller uiimagejpegrepresentation


    【解决方案1】:

    您可以使用从文档路径获取图像

    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
      }
    

    【讨论】:

      【解决方案2】:

      将 imagePath 保存在某处。然后,您可以使用以下方法获取图像:

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

      【讨论】:

        猜你喜欢
        • 2012-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-11
        • 2018-05-08
        • 2021-12-15
        • 1970-01-01
        • 2012-05-20
        相关资源
        最近更新 更多