简体   繁体   中英

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 the iPad, and swift playgrounds on the mac. here is the code:

import UIKit
import CoreImage
import CoreFoundation
import AVKit
import AVFoundation

let view = UIView()
var str = "Hello, playground"
import PlaygroundSupport
var captureSession: AVCaptureSession?
var stillImageOutput: AVCapturePhotoOutput?
var previewLayer: AVCaptureVideoPreviewLayer?







captureSession = AVCaptureSession()
captureSession!.sessionPreset = AVCaptureSessionPresetPhoto
var backCamera = AVCaptureDevice.defaultDevice(withMediaType:     AVMediaTypeVideo)
 var error: NSError?
 //var lol = AVCaptureInput();
 // var input = AVCaptureDeviceInput(device: backCamera, error: &error)

 var input = try AVCaptureDeviceInput(device: backCamera)

//error

if error == nil && captureSession!.canAddInput(input) {
    captureSession!.addInput(input)
    stillImageOutput = AVCapturePhotoOutput()
//    stillImageOutput?.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
    captureSession!.addOutput(stillImageOutput)

}

 previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
 view.layer.addSublayer(previewLayer!)
 previewLayer!.frame = view.bounds
 PlaygroundPage.current.liveView = view
 //PlaygroundPage.currentPage.liveView = view

One issue I can see with your code is that you never start the Capture Session. To this run use captureSession.startRunning()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM