相机预览图层未显示

时间:2017-03-24 22:04:23

标签: ios swift camera swift-playground

我在swift中使用自定义相机视图创建应用程序,但视图只是黑色 似乎没有显示实时预览窗口,这不适用于我的Xcode应用程序,iPad上的快速游乐场以及Mac上的快速游乐场。 这是代码:

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

1 个答案:

答案 0 :(得分:1)

我可以在您的代码中看到的一个问题是您从未启动过捕获会话。要运行captureSession.startRunning()