Swift clear camera preview layer
NickName:Damian Dudycz Ask DateTime:2017-04-11T16:30:18

Swift clear camera preview layer

I have an app that displays camera preview on AVCaptureVideoPreviewLayer. When app goes to background session is stopped. The problem is, that when I open the app again, then before new session is initialized, there is sill a preview from the time I stopped the session on the layer. How can I clean this preview on AVCaptureVideoPreviewLayer? I wan't it to contain just black background until new session is working.

Some parts of the code are bellow, but not all of it:

private var displayVideo = false {
    didSet {
        guard displayVideo != oldValue else { return }
        if displayVideo {
            // Configure video capture session.
            captureSession = {
                guard let captureDevice = self.captureDevice else { return nil }
                let captureSession = AVCaptureSession()
                captureSession.sessionPreset = AVCaptureSessionPresetHigh
                let videoDeviceInput = try! AVCaptureDeviceInput(device: captureDevice)
                guard captureSession.canAddInput(videoDeviceInput) else {
                    print("Warning. Failed to add input to capture session.")
                    return nil
                }
                captureSession.addInput(videoDeviceInput)
                configureVideoDevice()
                return captureSession
            }()
            configureVideoOrientation()
        }
        else { captureSession = nil }
    }
}

private var captureSession: AVCaptureSession? {
    didSet {
        if oldValue != captureSession { oldValue?.stopRunning() }
        cameraPreviewLayer.session = captureSession
        captureSession?.startRunning()
    }
}
private lazy var captureDevice: AVCaptureDevice? = {
    guard let videoDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) else {
        print("Warning. Failed to get capture device.")
        return nil
    }
    return videoDevice
}()
private lazy var cameraPreviewLayer: AVCaptureVideoPreviewLayer = {
    let layer = self.layer as! AVCaptureVideoPreviewLayer
    layer.videoGravity = AVLayerVideoGravityResizeAspectFill
    return layer
}()

Copyright Notice:Content Author:「Damian Dudycz」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43340608/swift-clear-camera-preview-layer

More about “Swift clear camera preview layer” related questions

Swift clear camera preview layer

I have an app that displays camera preview on AVCaptureVideoPreviewLayer. When app goes to background session is stopped. The problem is, that when I open the app again, then before new session is

Show Detail

Swift clear camera preview layer

I have an app that displays camera preview on AVCaptureVideoPreviewLayer. When app goes to background session is stopped. The problem is, that when I open the app again, then before new session is

Show Detail

only detect in a section of camera preview layer, iOS, Swift

I am trying to get a detection zone in a live preview on my camera preview layer. Is it possible for this, say there is a live feed and you have face detect on and as you look around it will only ...

Show Detail

Camera preview layer does not show up Swift 4

I am creating a ViewController in which I want to have a somewhat small UIView in the corner of the ViewController to display the camera preview. I am using a function to do this. However when I pa...

Show Detail

Camera Preview Layer not showing up

I am Creating an App with a Custom Camera View in swift but the view is just black it seems that thelive preview window is not showing up, This is not working in my Xcode app, swift playgrounds on ...

Show Detail

Camera preview layer makes button invisible

I'm trying to add a button over a camera preview but it doesn't show up when I run the program (I have constraints). I looked into the code and tried to debug but I'm new to swift and Xcode and I'm...

Show Detail

Mirroring (flipping) camera preview layer

So I am using AVCaptureSession to take pictures with front camera. I am also creating previewLayer from this session to display current image on screen. previewLayer = AVCaptureVideoPreviewLayer(s...

Show Detail

AVFoundation camera preview layer not working

So, I am trying to implement a camera using AVFoundation. I think I do everything right. this is what i am doing create session get devices of video type loop through devices to get the camera at ...

Show Detail

Placing controls above the layer in the preview using the plugin cordova-plugin-camera-preview

I am trying to use this photo camera plugin cordova-plugin-camera-preview in an HTML hybrid application and it works, but I can not place the controls on the camera layer while I focus. However, they

Show Detail

Add camera preview as layer for Mapview in Android

Currently, I'm working on a App which contain Map. in this I would like to add my camera preview as Mapview's background or layer. which means, All place markers shows only one my camera preview. .

Show Detail