簡體   English   中英

如何將從UIImagePickerController中獲取的圖像保存為HEIF文件?

[英]How to save image taken from UIImagePickerController as HEIF file?

如何將從UIImagePickerController獲取的圖像視為HEIF文件? 現在使用相機作為源類型,UIImagePickerControllerOriginalImage只為我提供了UIImage。 我想以HEIF格式將圖像發送到服務器。 這是假設imageExportPreset已設置為當前。

舊問題,但根據您發送數據的方式,您有兩個選擇。

  1. 將UIImage寫入物理文件並發送。

     do { let ctx = CIContext() let ciImage = CIImage(image: uiImage) try ctx.writeHEIFRepresentation(of: ciImage!, to: url, format: kCIFormatRGBA8, colorSpace: ctx.workingColorSpace!, options: [:]) } catch { print("ERROR", error.localizedDescription) } 
  2. 將UIImage寫入數據並發送

     let ctx = CIContext() let ciImage = CIImage(image: uiImage) let data = ctx.heifRepresentation(of: ciImage!, format: kCIFormatRGBA8, colorSpace: ctx.workingColorSpace!, options: [:]) 

我只能用sverin的解決方案調整的是不使用ctx.workingColorSpace,因為你可能正在改變圖像實際所在的空間。例如,你可能會注意到如果你將圖像重新加載到UIImageView中它看起來很褪色。

還為Swift 4更新了。

do {

    let ctx = CIContext()
    let ciImage = CIImage(image: uiImage)
    try ctx.writeHEIFRepresentation(of: ciImage!, to: url, format: .RGBA8, colorSpace: ciImage.colorSpace!, options: [:])

} catch {
    print("ERROR", error.localizedDescription)
}

暫無
暫無

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

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