[英]After applying CIFilter image got bigger no matter what I do
我閱讀了有關將CIFilter應用於UIImage並顯示回去的最新知識。 但是我仍然獲得比原始圖像更大的圖像,不知道為什么。 也許我缺少一些縮放因子?
這是我的屏幕,帶有普通的UIImageView和一些圖像。 唯一的限制是中心X / Y到超級視圖。
應用過濾器之前的屏幕截圖
但是我已經在viewDidLoad()
添加了以下代碼:
let ciContext = CIContext(options: nil)
let coreImage = CIImage(image: ledImageView.image!)
let filter = CIFilter(name: "CIExposureAdjust")
filter!.setValue(coreImage, forKey: kCIInputImageKey)
filter!.setValue(1.5, forKey: kCIInputEVKey)
let filteredImageData = filter?.outputImage as! CIImage
let filteredImageRef = ciContext.createCGImage(filteredImageData, from: filteredImageData.extent)
ledImageView.image = UIImage(cgImage: filteredImageRef!)
我得到的結果與預期不同(是的,已應用過濾器,但大小已損壞)。 我做錯了什么?
應用過濾器后的屏幕截圖
那很奇怪,
輸入和輸出圖像的延伸值是多少? 他們匹配嗎?
你可以試試這個
// crop the output image to the input's image extend
let croppedImage = filteredImageData.cropped(to: coreImage.extent)
let result = UIImage(ciImage: croppedImage)
我發現了問題的根源和解決方案。 顯然,最終的UIImage缺少scale
和imageOrientation
。 原始(源)圖像的比例尺== 3.0,而經過處理的圖像則保持比例尺== 1.0 。
這是正確的源代碼:
let ciContext = CIContext(options: nil)
let coreImage = CIImage(image: sourceImageView.image!)
let srcScale = sourceImageView.image.scale // <-- keep this value
let srcOrientation = sourceImageView.image.imageOrientation // <-- keep that value
let filter = CIFilter(name: "CIExposureAdjust")
filter!.setValue(coreImage, forKey: kCIInputImageKey)
filter!.setValue(1.5, forKey: kCIInputEVKey)
let filteredImageData = filter?.outputImage as! CIImage
let filteredImageRef = ciContext.createCGImage(filteredImageData, from: filteredImageData.extent)
// use this constructor with scale/orientation values
ledImageView.image = UIImage(cgImage: filteredImageRef!, scale: srcScale: orientation: srcOrientation)
現在結果如下:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.