簡體   English   中英

將 CIFilter 應用於 UIImage 會導致調整大小和重新定位圖像

[英]Applying CIFilter to UIImage results in resized and repositioned image

將 CIFilter 應用於相機拍攝的照片后,拍攝的圖像會縮小並重新定位。

我在想,如果我能夠獲得原始圖像的大小和方向,它會相應地縮放並將圖像視圖固定到屏幕的角落。 但是,這種方法沒有任何改變,並且不知道我可以正確地將圖像縮放到屏幕的全尺寸的方法。

func applyBloom() -> UIImage {
  let ciImage = CIImage(image: image) // image is from UIImageView

  let filteredImage = ciImage?.applyingFilter("CIBloom",
                                              withInputParameters: [ kCIInputRadiusKey: 8,
                                                                     kCIInputIntensityKey: 1.00 ])
  let originalScale = image.scale
  let originalOrientation = image.imageOrientation


  if let image = filteredImage {
    let image = UIImage(ciImage: image, scale: originalScale, orientation: originalOrientation)
    return image
  }

  return self.image
}

圖片說明:圖片截取並截屏,圖片縮小后留空。

在此處輸入圖片說明

嘗試這樣的事情。 代替:

func applyBloom() -> UIImage {
    let ciInputImage = CIImage(image: image) // image is from UIImageView
    let ciOutputImage = ciInputImage?.applyingFilter("CIBloom",
                                          withInputParameters: [kCIInputRadiusKey: 8, kCIInputIntensityKey: 1.00 ])
    let context = CIContext()
    let cgOutputImage = context.createCGImage(ciOutputImage, from: ciInputImage.extent)
    return UIImage(cgImage: cgOutputImage!)
}
  • 我保留了各種變量來幫助解釋正在發生的事情。
  • 顯然,根據您的代碼,可能需要對選項和展開進行一些調整。

發生的事情是這樣的 - 獲取過濾/輸出 CIImage,並使用 CIContext,編寫輸入 CIImage 大小的 CGImage。

  • 請注意 CIContext 是昂貴的。 如果您已經創建了一個,您可能應該使用它。
  • 幾乎, UIImage大小與 CIImage extent相同。 (我這么說是因為一些生成的 CIImages 可以有無限的范圍。)
  • 根據您的特定需求(以及您的 UIImageView),您可能希望改用輸出 CIImage 范圍。 但通常情況下,它們是相同的。

最后,一個建議。 如果您嘗試使用 CIFilter 來顯示對圖像的“近實時”更改(如照片編輯器),請考慮使用 CIImages 和GLKView對 UIImages 和UIImageView的主要性能改進。 前者使用設備 GPU 而不是 CPU。

如果CIFilter輸出尺寸與輸入圖像不同的圖像(例如使用CIPixellate ),也會發生這種情況

在這種情況下,只需告訴CIContext以較小的矩形呈現圖像:

let cgOutputImage = context.createCGImage(ciOutputImage, from: ciInputImage.extent.insetBy(dx: 20, dy: 20))

暫無
暫無

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

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