我在使用CIFilter时遇到了一个问题,它在输出的图像上面和下面应用了一个偏移量/边框。它可以在下面的例子中看到。

在本例中,我只需使用一个UIImage,应用CIPixellate,固定方向,然后在全屏UIImageView中显示该图像,其中背景颜色为红色,以演示该问题(将背景颜色设置为默认颜色‘清除’会产生白色偏移。下面是UIImageView的设置。

通过调试,我知道这个问题发生在应用CIFilter的某个地方,并且它与UIImageView无关,如果我只是在没有过滤器的情况下传递图像,那么这个问题就不会发生。
作为参考,下面是我用来对图像进行像素化的代码:
extension UIImage {
func pixellate(amount: Int) -> UIImage? {
let ciImage = CIImage(cgImage: self.cgImage!)
let filter = CIFilter(name: "CIPixellate")
filter?.setValue(ciImage, forKey: kCIInputImageKey)
filter?.setValue(amount, forKey: kCIInputScaleKey)
guard let outputImage = filter?.outputImage else { return nil }
var pixellatedImage: UIImage?
// Fix the orientation of the newly processed image
let context = CIContext()
if let cgimg = context.createCGImage(outputImage, from: outputImage.extent) {
let processedImage = UIImage(cgImage: cgimg)
let portraitImage = UIImage(cgImage: processedImage.cgImage!, scale: 1.0, orientation: UIImageOrientation.right)
pixellatedImage = portraitImage
}
return pixellatedImage
}
}这里发生了什么事?有办法阻止这件事吗?
发布于 2018-04-10 20:02:46
在计算目标像素时,许多图像滤波器必须包括相邻像素。当然,这会在边缘引起问题。一种可能是在边缘重复像素。苹果提供了一种方便的方法:clampedToExtent
叫这个方法..。通过从原始图像的边缘重复像素颜色来创建无限范围的图像。
...
let ciImage = CIImage(cgImage: self.cgImage!).clampedToExtent()https://stackoverflow.com/questions/49758676
复制相似问题