我阅读了有关将 CIFilter 应用于 UIImage 并将其显示回来的最新知识。但是我的图像仍然比原来的大,不知道为什么。也许我遗漏了一些比例因子?
这是我的屏幕,带有纯 UIImageView 和一些图像。唯一的约束是 super View 的中心 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!)
我得到了与预期不同的结果(是的,应用了过滤器,但大小被破坏了)。我做错了什么?
应用过滤器后的截图
最佳答案
我找到了问题的根本原因和解决方案。显然最终的 UIImage 缺少 scale
和 imageOrientation
。原始(源)图像具有 scale == 3.0,而处理后的图像保持 scale == 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)
现在结果如下:)
关于ios - 应用 CIFilter 之后,无论我做什么,图像都会变大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52090405/