Ios 使用CIFilter围绕中心旋转CIImage
我正在尝试旋转CMSampleBuffer(来自相机)的像素数据,并将其显示在UIImageView中。我不想旋转视图本身。它必须是实际的像素数据 我抓取提要,将其转换为CIImage,然后用CIFilter旋转它。 我在设置旋转点时遇到问题。 现在它围绕左下角点旋转。我想绕着它的中心旋转Ios 使用CIFilter围绕中心旋转CIImage,ios,swift,core-graphics,cifilter,ciimage,Ios,Swift,Core Graphics,Cifilter,Ciimage,我正在尝试旋转CMSampleBuffer(来自相机)的像素数据,并将其显示在UIImageView中。我不想旋转视图本身。它必须是实际的像素数据 我抓取提要,将其转换为CIImage,然后用CIFilter旋转它。 我在设置旋转点时遇到问题。 现在它围绕左下角点旋转。我想绕着它的中心旋转 func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffe
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
//create pixelbuffer
let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
let context = CIContext.init(options: nil)
// calculating the radians to rotate the ciimage by
let radians : Float = atan2f(Float(boxView!.transform.b), Float(boxView!.transform.a));
//create ciimage from the pixelbuffer
let options = [kCVPixelBufferCGImageCompatibilityKey as String: true,
kCVPixelBufferCGBitmapContextCompatibilityKey as String: true,]
let ci = CIImage.init(cvPixelBuffer: pixelBuffer, options: options)
//transform filter
let filter = CIFilter.init(name: "CIAffineTransform")
//set translation point to center
var transform = CGAffineTransform(translationX: self.view.frame.midX, y: self.view.frame.midY)
//rotate
transform = CGAffineTransform(rotationAngle:CGFloat(radians))
// set translation point back again to normal
var transformBack = CGAffineTransform(translationX: -self.view.frame.midX, y: -self.view.frame.midY)
//applying the transform
transform.concatenating(transformBack)
filter!.setValue(transform, forKey: kCIInputTransformKey)
filter!.setValue(ci, forKey: kCIInputImageKey)
let output = filter?.outputImage
let img = context.createCGImage(output!, from: imageView!.frame)
// send image to be displayed
self.imageView!.image = UIImage(cgImage: img!)
}
找到了。我必须对转换变量本身应用转换
let transform = CGAffineTransform(translationX: self.view.frame.midX, y: self.view.frame.midY).rotate(angle: CGFloat(radians)).translatedBy(x: -self.view.frame.midX, y: -self.view.frame.midY)
Swift 5:
let image = CIImage(cvPixelBuffer: yourBuffer)
let rotatedImage = image.oriented(.right)
谢谢你的帮助。在寻找一个更简单的解决方案之前,我对三角学失去了兴趣。平移、旋转和向后平移就足够了。也许在数学方面,它可能会表现得更好,但就目前而言,这将起到作用。:)再次感谢你!!我认为答案是关于CIImage,而不是UIView,这可能会让读者感到困惑。你是什么意思?我的问题是关于CIImage的,我明确表示我想旋转CIImage而不是UIView,所以我的答案是正确的。非常简单。谢谢正是我需要的。