簡體   English   中英

Swift 3.0 使用 write(toFile: atomically:) 寫入二進制圖像數據

[英]Swift 3.0 Writing binary image data with write(toFile: atomically:)

我正在完成教程Project10並嘗試將其轉換為 Swift 3.0。 使用UIImagePickerController選擇圖像然后將其保存到Documents Directory是通常直接的情況。

我在這一行遇到錯誤:

jpegData.write(toFile: imagePath, atomically: true)

生命的開始是這樣的:

jpegData.writeToFile(imagePath, atomically: true)

該錯誤想用以下內容替換我的論點:

jpegData.write(to: imagePath, options: true)

我很確定這不是我想要的,無論如何它會導致進一步的字符串/URL 錯誤。 這兩種方法的完整內容是:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    var newImage: UIImage

    if let possibleImage = info[UIImagePickerControllerEditedImage] as? UIImage {
        newImage = possibleImage
    } else if let possibleImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        newImage = possibleImage
    } else {
        return
    }

    let imageName = NSUUID().uuidString
    let imagePath = getDocumentsDirectory().appendingPathComponent(imageName)

    if let jpegData = UIImageJPEGRepresentation(newImage, 80) {
        jpegData.write(toFile: imagePath, atomically: true)
    }

    dismiss(animated: true, completion: nil)
}

func getDocumentsDirectory() -> NSString {
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

我只是對字符串、NSStrings 和 URL 感到困惑嗎?

我在以下方面取得了一些進展:

let imagePath = getDocumentsDirectory().appendingPathComponent(imageName)
let path = URL(fileURLWithPath: imagePath)
if let jpegData = UIImageJPEGRepresentation(newImage, 80) {
   try! jpegData.write(to: path, options: [.atomic])
}

丑陋和松弛可能的錯誤(現在)但使用El Capitan的建議方法版本並將String轉換為URL讓我前進。

此代碼適用於Swift 3:

  if let jpegData = UIImageJPEGRepresentation(iconImage!, 80) {
      do {
            try jpegData.write(to: imagePath!)

         } catch {
             print("write image failed")       
         }
   }

在Swift 4.1和Xcode 9.4.1中

這是完美的解決方案,我們需要嘗試使用

do {
    try data.write(to: URL(fileURLWithPath: path), options: .atomic)
} catch {
    print(error)
}
if (UIImage(data: data!) != nil)
            {
                let Image = UIImage(data: data!) //image data

                self.ImageView.image = Image //Image view

                let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
                if documentsPath.count > 0{
                    let documentsDirectory = documentsPath[0]
                    let savePath = documentsDirectory + "/name.jpg"
                    do{
                        try UIImageJPEGRepresentation(Image!, 1)?.writeToURL(NSURL(fileURLWithPath: savePath), atomically: true)
                    }catch{
                        //process errors
                    }
                }
            }

您可以將移動中的圖像存儲在您的移動應用中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM