所以大家,因为我有多个视图和一些缩放按钮,我需要它们来更改预览相机输出,我认为使用 Singleton 进行会话初始化是正确的,但我没有想法怎么做,找不到任何好的信息,有人可以帮我吗?
答案 0 :(得分:0)
<强>更新强>
好的,所以,我设法以某种方式写它,我不知道它是否可以,这是代码:protocol Singleton: class {
static var sharedInstance: Self { get }
}
final class AVFSessionSingleton: Singleton {
static let sharedInstance = AVFSessionSingleton()
private init() {
session = newVideoCaptureSession()!
}
var session: AVCaptureSession!
var imageOutput : AVCaptureStillImageOutput?
//FUNCTION
func newVideoCaptureSession () -> AVCaptureSession? {
func initCaptureDevice() -> AVCaptureDevice? {
var captureDevice: AVCaptureDevice?
let devices: NSArray = AVCaptureDevice.devices() as NSArray
for device: Any in devices {
if (device as AnyObject).position == AVCaptureDevicePosition.back {
captureDevice = device as? AVCaptureDevice
}
}
print("device inited")
return captureDevice
}
func initOutput() {
self.imageOutput = AVCaptureStillImageOutput()
}
func initInputDevice(captureDevice : AVCaptureDevice) -> AVCaptureInput? {
var deviceInput : AVCaptureInput?
do {
deviceInput = try AVCaptureDeviceInput(device: captureDevice)
}
catch _ {
deviceInput = nil
}
return deviceInput
}
func initSession(deviceInput: AVCaptureInput) {
self.session = AVCaptureSession()
self.session?.sessionPreset = AVCaptureSessionPresetPhoto
self.session?.addInput(deviceInput)
self.session?.addOutput(self.imageOutput!)
}
return session
}
}
所以现在我想以这种方式调用它来管理预览版的布局,请提出任何建议吗?....