AVCaptureSession with multiple previews

后端 未结 5 1072
花落未央
花落未央 2020-11-28 21:48

I have an AVCaptureSession running with an AVCaptureVideoPreviewLayer.

I can see the video so I know it\'s working.

However, I\'d like to have a collection v

5条回答
  •  暖寄归人
    2020-11-28 22:11

    Working in Swift 5 on iOS 13, I implemented a somewhat simpler version of the answer by @Ushan87. For testing purposes, I dragged a new, small UIImageView on top of my existing AVCaptureVideoPreviewLayer. In the ViewController for that window, I added an IBOutlet for the new view and a variable to describe the correct orientation for the camera being used:

        @IBOutlet var testView: UIImageView!
        private var extOrientation: UIImage.Orientation = .up
    

    I then implemented the AVCaptureVideoDataOutputSampleBufferDelegate as follows:

    // MARK: - AVCaptureVideoDataOutputSampleBufferDelegate
    extension CameraViewController: AVCaptureVideoDataOutputSampleBufferDelegate {
        func captureOutput(_ captureOutput: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    
            let imageBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
            let ciimage : CIImage = CIImage(cvPixelBuffer: imageBuffer)
            let image : UIImage = self.convert(cmage: ciimage)
    
            DispatchQueue.main.sync(execute: {() -> Void in
                testView.image = image
            })
    
        }
    
        // Convert CIImage to CGImage
        func convert(cmage:CIImage) -> UIImage
        {
            let context:CIContext = CIContext.init(options: nil)
            let cgImage:CGImage = context.createCGImage(cmage, from: cmage.extent)!
            let image:UIImage = UIImage.init(cgImage: cgImage, scale: 1.0, orientation: extOrientation)
            return image
        }
    

    For my purposes, the performance was fine. I did not notice any lagginess in the new view.

提交回复
热议问题