我正在使用下面的代码将图像滤镜应用于具有设定半径的触摸位置,但滤镜位于错误的位置。 x坐标似乎很接近,但y坐标反转,距离我的触摸有400多个像素。如果我将手指向上移动,则过滤器向下移动,反之亦然。我是否错误地使用了kCIInputCenterKey:CIVector参数?如何将滤镜半径的中心放在触摸位置?
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let filter = CIFilter(name: "CIPixellate")!
let inputImage = CIImage(image: originalImage)!
filter.setValue(inputImage, forKey: kCIInputImageKey)
let fullPixellatedImage = filter.outputImage
var maskImage: CIImage!
let radialGradient = CIFilter(name: "CIRadialGradient",
withInputParameters: [
"inputRadius0" : 30,
kCIInputCenterKey : CIVector(x: (touches.first?.locationInView(self.view).x)!, y: (touches.first?.locationInView(self.view).y)!)
])!
let radialGradientOutputImage = radialGradient.outputImage!.imageByCroppingToRect(inputImage.extent)
if maskImage == nil {
maskImage = radialGradientOutputImage
} else {
maskImage = CIFilter(name: "CISourceOverCompositing",
withInputParameters: [
kCIInputImageKey : radialGradientOutputImage,
kCIInputBackgroundImageKey : maskImage
])!.outputImage
}
let blendFilter = CIFilter(name: "CIBlendWithMask")!
blendFilter.setValue(fullPixellatedImage, forKey: kCIInputImageKey)
blendFilter.setValue(inputImage, forKey: kCIInputBackgroundImageKey)
blendFilter.setValue(maskImage, forKey: kCIInputMaskImageKey)
let blendOutputImage = blendFilter.outputImage!
let blendCGImage = context.createCGImage(blendOutputImage, fromRect: blendOutputImage.extent)
mainImageView.image = UIImage(CGImage: blendCGImage)
}