Reputation: 31
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
Upvotes: 0
Views: 1975
Reputation: 41
One issue I can see with your code is that you never start the Capture Session. To this run use captureSession.startRunning()
Upvotes: 1