[英]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.