ios - 使用 CIFilter 围绕中心旋转 CIImage

标签 ios swift core-graphics cifilter ciimage

我正在尝试旋转 CMSampleBuffer(来自相机)的像素数据并将其显示在 UIImageView 中。我不想旋转 View 本身。它必须是实际的像素数据。

我抓取提要,将其转换为 CIImage,然后使用 CIFilter 旋转它。 我在设置必须围绕其旋转的点时遇到问题。 现在它围绕左下角旋转。我想围绕它的中心旋转它。

 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!)

}

最佳答案

swift 5:

let image = CIImage(cvPixelBuffer: yourBuffer)
let rotatedImage = image.oriented(.right)

Docs

关于ios - 使用 CIFilter 围绕中心旋转 CIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44500622/

相关文章:

iphone - iPhone游戏中的简单菜单

ios - 直接使用 Realm 对象还是映射到对象类?

ios - 如何使搜索栏像 Iphone 设置

ios - 如何将 UIScrollView 内容大小配置为具有固定宽度和动态高度?

ios - 带有 Swift 的 Firebase 对 observeEventType 的模糊使用

ios - 链接 SceneKit Framework 避免在 iOS 7 上崩溃

ios - UIBezierPath 和 NSBezierPath 在绘制圆弧时产生不同的结果

ios - 在 iPhone 4/4S 和 iPad 2 上生成 54 兆像素图像

ios - Apple pay PKPaymentauthorizationViewController 在加载付款请求时总是返回 nil

ios - 我把NotificationCenter.default.addObserver放在哪里,以便可以从框架的任何点调用它?