gpt4 book ai didi

ios - 尝试根据触摸位置为图像过滤器制作 kCIInputCenterKey 并翻转 y 坐标

转载 作者:行者123 更新时间:2023-11-29 00:24:35 25 4
gpt4 key购买 nike

如果有人帮助我将 X 和 Y 触摸坐标转换为正确的 CIVectors,我将不胜感激。下面的代码是我用来使“触摸”成为凹凸失真的 kCIInputCenterKey 坐标的所有代码。它有点工作,但是当我触摸屏幕以应用选择滤镜的中心键时,Y 坐标会翻转。 X 是正确的,但如果我触摸图像的顶部,过滤器将应用于图像的相对较低部分,同时保持正确的 x 轴位置

var xCord:CGFloat = 0.0
var yCord:CGFloat = 0.0

func didTapImage(gesture: UIGestureRecognizer) {
let point = gesture.location(in: gesture.view)
print(point)

xCord = point.x
yCord = point.y

print ("\(point) and x\(xCord) and \(yCord)")
}

@IBAction func filter(_ sender: Any) {

guard let image = self.imageView.image?.cgImage else { return }

let openGLContext = EAGLContext(api: .openGLES3)
let context = CIContext(eaglContext: openGLContext!)
let ciImage = CIImage(cgImage: image)

let filter = CIFilter(name: "CIBumpDistortion")

filter?.setValue(ciImage, forKey: kCIInputImageKey)
filter?.setValue((CIVector(x: xCord, y: yCord)), forKey: kCIInputCenterKey)
filter?.setValue(300.0, forKey: kCIInputRadiusKey)
filter?.setValue(2.50, forKey: kCIInputScaleKey)

centerScrollViewContents()


if let output = filter?.value(forKey: kCIOutputImageKey) as? CIImage{
self.imageView.image = UIImage(cgImage: context.createCGImage(output, from: output.extent)!)
}


}

问题是 yCord 由于某种原因被翻转,从 float 变为向量。当我尝试使用类似的东西来纠正这个问题时:

filter?.setValue((CIVector(x: xCord, y: -yCord)), forKey: kCIInputCenterKey)

filter?.setValue((CIVector(x: xCord, y: yCord * (-1))), forKey: kCIInputCenterKey)

它会导致整个图像在 ImageView 中上下跳动,并且滤镜不会应用于任何地方。不知道从这里去哪里,因为这个值不想用简单的数学来翻转。任何帮助将不胜感激!

最佳答案

我找到了解决方案。

要正确翻转 y,我所要做的就是:

filter?.setValue((CIVector(x: xCord, y: CGFloat(image.height) - yCord)), forKey: kCIInputCenterKey) 

关于ios - 尝试根据触摸位置为图像过滤器制作 kCIInputCenterKey 并翻转 y 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43428573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com