private func capturesetup (position : AVCaptureDeviceposition) { var captureError : NSError? var captureDevice : AVCaptureDevice! for testedDevice in AVCaptureDevice.devicesWithMediaType(AVMediaTypeVIDeo){ if (testedDevice.position == position) { captureDevice = testedDevice as! AVCaptureDevice } } if (captureDevice == nil) { captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVIDeo) } var deviceinput : AVCaptureDeviceinput do { deviceinput = try AVCaptureDeviceinput(device: captureDevice) } catch let error as NSError { captureError = error deviceinput = nil // Nil cannot be assigned to type AVCaptureDeviceinput if captureError != nil { // new if print("error: \(captureError?.localizedDescription)") } } captureSession.sessionPreset = AVCaptureSessionPresetHigh if (captureError == nil) { if (captureSession.canAddinput(deviceinput)) { captureSession.addinput(deviceinput) } self.vIDeoDataOutput = AVCaptureVIDeoDataOutput() self.vIDeoDataOutput!.vIDeoSettings = [kCVPixelBufferPixelFormatTypeKey: Int(kCVPixelFormatType_32BGRA)] self.vIDeoDataOutput!.alwaysdiscardsLateVIDeoFrames = true self.vIDeoDataOutputQueue = dispatch_queue_create("VIDeoDataOutputQueue",disPATCH_QUEUE_SERIAL) self.vIDeoDataOutput!.setSampleBufferDelegate(self,queue: self.vIDeoDataOutputQueue!) if (captureSession.canAddOutput(self.vIDeoDataOutput)) { captureSession.addOutput(self.vIDeoDataOutput) } } visageCameraview.frame = UIScreen.mainScreen().bounds let prevIEwLayer: AVCaptureVIDeoPrevIEwLayer = AVCaptureVIDeoPrevIEwLayer(session: captureSession) as AVCaptureVIDeoPrevIEwLayer // changed to let prevIEwLayer.frame = UIScreen.mainScreen().bounds prevIEwLayer.vIDeoGravity = AVLayerVIDeoGravityResizeAspectFill visageCameraview.layer.addSublayer(prevIEwLayer)}解决方法 这对我来说很有意义.如果你想把它设置为零.使用选项.这样你就可以把它设置为零.这是一个很好的机制来避免崩溃和大量的if语句进行空检查.我推荐的(即使是测试)是使用选项.这就是你的样子:
var deviceinput : AVCaptureDeviceinput?
现在,deviceinput不必保存任何有效值.所以你可以写下面的内容(虽然默认为nil)
deviceinput = nil
现在,假设您想在其他地方使用它.你可以使用如果像这样:
if let myNonNilValue = deviceinput { // myNonNilValue is for sure not nil}总结
以上是内存溢出为你收集整理的swift – 无法将Nil分配给AVCaptureDeviceInput类型全部内容,希望文章能够帮你解决swift – 无法将Nil分配给AVCaptureDeviceInput类型所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)