How to get current picture from WebRTC as image?

Viewed 325

I am using WebRTC to capture video from user camera. In some place I want to get current picture as UIImage for saving it in photo library. I am using localVideoView to show video from local camera, but when I try to make screenshot of that view, it is empty (just blue background).

This is my code to make screenshot:

func screenShotMethod() {
    DispatchQueue.main.async {
        //Create the UIImage
        UIGraphicsBeginImageContext(self.localVideoView!.frame.size)
        self.localVideoView?.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        //Save it to the camera roll
        UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
    }
}
1 Answers

Here is the sample code to capture photo without camera preview during a WebRTC video call. Have to invoke TakePicture() method through signal or button tap.

import AVFoundation
import WebRTC
import UIKit
import Foundation
class CallViewController: UIViewController{
var captureSession : AVCaptureSession?
   func TakePicture() {
        DispatchQueue.main.async { [self] in
            captureSession = AVCaptureSession()
            captureSession!.beginConfiguration()
            let photoOutput = AVCapturePhotoOutput()
            photoOutput.isHighResolutionCaptureEnabled = true
            photoOutput.isLivePhotoCaptureEnabled = false
            if let captureDevice = AVCaptureDevice.default(for: .video){
                do
                {
                    let input = try AVCaptureDeviceInput(device: captureDevice)
                    if captureSession!.canAddInput(input){
                        captureSession!.addInput(input)
                    }
                } catch let error {
                    
                }
                
                if captureSession!.canAddOutput(photoOutput){
                    captureSession!.addOutput(photoOutput)
                }
                
                let cameraLayer = AVCaptureVideoPreviewLayer()
                cameraLayer.session = captureSession
                
                captureSession!.commitConfiguration()
                captureSession!.startRunning()
                
                let photoSettings = AVCapturePhotoSettings()
                //photoSettings.flashMode = .auto //check device properties before turning on flash
                photoSettings.photoQualityPrioritization = .balanced
                photoOutput.capturePhoto(with: photoSettings, delegate: self)
                
            }
        }
    }
}
extension CallViewController: AVCapturePhotoCaptureDelegate{
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        captureSession!.stopRunning()
        captureSession = nil
        let imageData = photo.fileDataRepresentation()
        //Do the rest with image bytes
        
    }
}
Related