0
我試圖旋轉CMSampleBuffer(來自相機)的像素數據並將其顯示在UIImageView中。我不想旋轉視圖本身。它必須是實際的像素數據。使用CIFilter圍繞中心旋轉CIImage
我抓住feed,將其轉換爲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!)
}