Swift 3 - 如何捕获图像及其子视图?

Swift 3 - How to capture image and its subview?

我正在处理一个包含自定义相机的项目view.And我无法在主视图上捕获子视图view.My主视图由AVCaptureSession组成,我想拍照单张图片中的父视图和子视图。

我在代码中尝试的内容:

class ViewController: UIViewController {

@IBOutlet weak var cameraButton: UIButton!

var captureSession = AVCaptureSession()

var backCamera: AVCaptureDevice?
var frontCamera: AVCaptureDevice?
var currentDevice: AVCaptureDevice?

var photoOutput: AVCapturePhotoOutput?

var cameraPreviewLayer:AVCaptureVideoPreviewLayer?

var image: UIImage?

var toggleCameraGestureRecognizer = UISwipeGestureRecognizer()

var zoomInGestureRecognizer = UISwipeGestureRecognizer()
var zoomOutGestureRecognizer = UISwipeGestureRecognizer()

override func viewDidLoad() {
    super.viewDidLoad()
    setupCaptureSession()
    setupDevice()
    setupInputOutput()
    setupPreviewLayer()
    captureSession.startRunning()

    toggleCameraGestureRecognizer.direction = .up
    toggleCameraGestureRecognizer.addTarget(self, action: #selector(self.switchCamera))
    view.addGestureRecognizer(toggleCameraGestureRecognizer)

    // Zoom In recognizer
    zoomInGestureRecognizer.direction = .right
    zoomInGestureRecognizer.addTarget(self, action: #selector(zoomIn))
    view.addGestureRecognizer(zoomInGestureRecognizer)

    // Zoom Out recognizer
    zoomOutGestureRecognizer.direction = .left
    zoomOutGestureRecognizer.addTarget(self, action: #selector(zoomOut))
    view.addGestureRecognizer(zoomOutGestureRecognizer)
    styleCaptureButton()
}
  cameraButton.layer.borderColor = UIColor.white.cgColor
    cameraButton.layer.borderWidth = 5
    cameraButton.clipsToBounds = true
    cameraButton.layer.cornerRadius = min(cameraButton.frame.width, cameraButton.frame.height) / 2
}

func setupCaptureSession() {
    captureSession.sessionPreset = AVCaptureSession.Preset.photo
}

func setupDevice() {
    let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
    let devices = deviceDiscoverySession.devices

    for device in devices {
        if device.position == AVCaptureDevice.Position.back {
            backCamera = device
        } else if device.position == AVCaptureDevice.Position.front {
            frontCamera = device
        }
    }
    currentDevice = backCamera
}

func setupInputOutput() {
    do {

        let captureDeviceInput = try AVCaptureDeviceInput(device: currentDevice!)
        captureSession.addInput(captureDeviceInput)
        photoOutput = AVCapturePhotoOutput()
        photoOutput!.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey : AVVideoCodecType.jpeg])], completionHandler: nil)
        captureSession.addOutput(photoOutput!)


    } catch {
        print(error)
    }
}

func setupPreviewLayer() {
    self.cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    self.cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    self.cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
    self.cameraPreviewLayer?.frame = view.frame

    self.view.layer.insertSublayer(self.cameraPreviewLayer!, at: 0)
}
@IBAction func cameraButton_TouchUpInside(_ sender: Any) {
    let settings = AVCapturePhotoSettings()
    self.photoOutput?.capturePhoto(with: settings, delegate: self)
}
}
extension ViewController: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    if let imageData = photo.fileDataRepresentation() {
        self.image = UIImage(data: imageData)
        performSegue(withIdentifier: "Preview_Segue", sender: nil)
    }
}
}

请帮帮我

据我所知,您正在尝试获取相机正在抓取的内容(作为图像)和一些叠加视图。
据我所知,无法获取 AVPreviewLayer 中的内容,也许他们在最新版本中更改了一些内容。当我尝试 (iOS6) 时,这是不可能的,带有 AVPreviewLayer 的区域总是空的。
你可以做的是获取当前的相机缓冲区并在其中绘制。通过将 class 设置为会话委托,您可以接收此回调可选

func captureOutput(_ output: AVCaptureOutput, 
                  didOutput sampleBuffer: CMSampleBuffer, 
                       from connection: AVCaptureConnection)

这里你将从相机接收图像,这个缓冲区可以使用Accelerate框架或CoreImage转换成图像。
不容易,也不容易impossible.