我正在用swift创建一个带有自定义相机视图的应用程序,但视图只是黑色的
似乎实时预览窗口没有出现,这在我的Xcode应用程序、iPad上的swift playgrounds和mac上的swift playgrounds中都不起作用。
代码如下:
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
最佳答案
我可以从你的代码中看到一个问题,那就是你永远不会启动捕获会话。使用captureSession.startRunning()