How to instantly see new video layer after letting go of button?

送分小仙女□ 提交于 2019-12-05 15:03:39

This can be done in the following way. Set these properties in the viewController for storing the thumbnail of the first frame when video recording starts and a Boolean to store if the photo captured is for a thumbnail or a regular photo capture by single tap:

private var isSettingThumbnail = false
private var thumbImage: UIImage?

and change the implementation of the single tap gesture recognizer target

@objc func normalTap(_ sender: UIGestureRecognizer) {
    //self.numForPic = numForPic + 1
    let settings = AVCapturePhotoSettings()
    isSettingThumbnail = false
    photoOutput?.capturePhoto(with: settings, delegate: self)
}

You also need to remove the preview layer when you stop recording otherwise the captured thumb image cannot be shown on the ImageView. Here the 10 seconds delay is just to check if the thumb image shows. Only for debugging purposes you can get rid of it anytime later.

func stopRecording() {
        if thumbImage != nil {
            camPreview.image = thumbImage!
        }
        previewLayer?.removeFromSuperlayer()
        DispatchQueue.main.asyncAfter(deadline: .now() + 10.0, execute: {
            if self.movieOutput.isRecording == true {
                self.movieOutput.stopRecording()
            }
        })

    }

Now add the delegate function to AVCaptureFileOutputRecordingDelegate to get a callback when the recording starts. At this point, you set the boolean flag.

func fileOutput(_ output: AVCaptureFileOutput, didStartRecordingTo fileURL: URL, from connections: [AVCaptureConnection]) {
    isSettingThumbnail = true
    photoOutput?.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
}

Now we check with this boolean flag and store and store the thumbnail image or the normal image.

extension ViewController: AVCapturePhotoCaptureDelegate {

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {

        print("you in this !")

        if let imageData = photo.fileDataRepresentation() {
            print("\(UIImage(data: imageData)) <-- image DALUE FEFE DEDE KEKE LALY")
            if isSettingThumbnail {
                thumbImage = UIImage(data: imageData)
            } else {
                image = UIImage(data: imageData)
            }

            print("\(image) <-- dada dudu creoo IMAGE valu")
        }

    }

}

I have tested the code and is working fine. Also when a recording is in progress the shutter sound does not play by default and we take the photo when the recording has begun so we are doing it as per the Apple guidelines. According to this apple doc. This is one possible way of doing it. Also please change the class of Cam Preview to UIImageView in the storyboard. You can check the working code on github. Have a great day. It was a quite an interesting question. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!