简体   繁体   English

应用CIFilter后,无论我做什么图像都会变大

[英]After applying CIFilter image got bigger no matter what I do

I read latest know-hows about applying CIFilter to UIImage and display it back. 我阅读了有关将CIFilter应用于UIImage并显示回去的最新知识。 However I still get my image bigger than the original, don't know why. 但是我仍然获得比原始图像更大的图像,不知道为什么。 Maybe I'm missing some scaling factor?. 也许我缺少一些缩放因子?

This is my screen with plain UIImageView and some image. 这是我的屏幕,带有普通的UIImageView和一些图像。 The only constraints are center X/Y to superview. 唯一的限制是中心X / Y到超级视图。

Screenshot before applying filter 应用过滤器之前的屏幕截图

在此处输入图片说明

However I've added this code in viewDidLoad() : 但是我已经在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!)

I get a result other than expected (yes, the filter is applied but size is broken). 我得到的结果与预期不同(是的,已应用过滤器,但大小已损坏)。 What did I do wrong? 我做错了什么?

Screenshot after applying filter 应用过滤器后的屏幕截图

在此处输入图片说明

Thats strange, 那很奇怪,

what are the values of the extends of the input and output image ? 输入和输出图像的延伸值是多少? Do they match ? 他们匹配吗?

You could try this 你可以试试这个

// crop the output image to the input's image extend
let croppedImage = filteredImageData.cropped(to: coreImage.extent)
let result = UIImage(ciImage: croppedImage)

I found both root cause of the issue and the solution. 我发现了问题的根源和解决方案。 Apparently final UIImage was lacking of scale and imageOrientation . 显然,最终的UIImage缺少scaleimageOrientation The original (source) image had scale == 3.0 while image after processing stayed with scale == 1.0 . 原始(源)图像的比例尺== 3.0,而经过处理的图像则保持比例尺== 1.0

Here is the proper source code for this: 这是正确的源代码:

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)

Now the result is as bellow :) 现在结果如下:)

Fixed image on ViewController 在ViewController上固定图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM