Camera preview layer makes button invisible
NickName:Philip J Ask DateTime:2017-07-30T04:52:57

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 new to debugging in general. I saw that when I commented out the camera preview layer the button showed up. Thanks!

import UIKit
import AVFoundation
import QuartzCore

class View1: UIViewController , AVCaptureVideoDataOutputSampleBufferDelegate{

let captureSession = AVCaptureSession()
var previewLayer:CALayer!
var captureDevice:AVCaptureDevice!

@IBOutlet weak var cameraView:UIView!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    prepareCamera()
}

func prepareCamera() {
    captureSession.sessionPreset = AVCaptureSessionPreset1920x1080

    if let availableDevices = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInWideAngleCamera],                                                                                                          mediaType: AVMediaTypeVideo,                                                                                                             position: .back).devices {

        captureDevice = availableDevices.first
        beginSession()
    }
}

func beginSession() {
    do {
        let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice)
        captureSession.addInput(captureDeviceInput)
    } catch {
        print(error.localizedDescription)
        //Figure out what to do here 
 }
    if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) {



        self.previewLayer = previewLayer
        self.view.layer.addSublayer(self.previewLayer)
        self.previewLayer.frame = self.view.layer.frame
        captureSession.startRunning()

        let dataOutput = AVCaptureVideoDataOutput()
        dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString): NSNumber(value: kCVPixelFormatType_32BGRA)]

        dataOutput.alwaysDiscardsLateVideoFrames = true

        if captureSession.canAddOutput(dataOutput){

            captureSession.addOutput(dataOutput)
            captureSession.commitConfiguration()
        }

        let queue = DispatchQueue(label: "com.PhotoAllergy.captureQueue")
        dataOutput.setSampleBufferDelegate(self, queue: queue)
    }  
}

}

Copyright Notice:Content Author:「Philip J」,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/45393951/camera-preview-layer-makes-button-invisible

Answers
the_kaseys 2017-07-29T21:00:10

Maybe you could try setting the zPosition of the button to 1 or higher. YourButtonName.layer.zPostion = 2\n\nApple Documentation on ZPosition",


More about “Camera preview layer makes button invisible” related questions

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

Adding button makes camera preview not display

I have an app that has a camera preview view. I've added a button that overlays the FrameLayout that the camera preview is inside. Before I added the button the camera preview worked fine; however,...

Show Detail

Changing text on UILabel that is on top of camera preview layer makes it blink

I have been using AVFoundation to get preview from camera. Here is how i set it up: // create session _session = [[AVCaptureSession alloc] init]; _session.sessionPreset = AVCaptureSessionPresetPh...

Show Detail

Invisible SurfaceView for Camera Preview

I need to get camera preview data only, but not visible preview. Since I'm doing all this in a service, I had to create a dummy SurfaceView, which works very well. I've used the code from this ans...

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

Add subView to camera preview layer

I want to add a button in front of camera preview layer, the button work fine it print (1234) but i can't see it. I change the tint color to white but nothing change i still don't see it. i think ...

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

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