Ios 为什么AVCaptureSession方法CanadOutput返回false?
我正在尝试构建一个摄像头应用程序,并尝试在主视图控制器的viewDidLoad()中设置捕获会话。出于某种原因,每当我在手机上运行应用程序时,AVCaptureSession方法CanadOutput的计算结果都为false:Ios 为什么AVCaptureSession方法CanadOutput返回false?,ios,swift3,xcode8,avcapturesession,Ios,Swift3,Xcode8,Avcapturesession,我正在尝试构建一个摄像头应用程序,并尝试在主视图控制器的viewDidLoad()中设置捕获会话。出于某种原因,每当我在手机上运行应用程序时,AVCaptureSession方法CanadOutput的计算结果都为false: var captureSession: AVCaptureSession! var photoOutput: AVCapturePhotoOutput! var previewLayer : AVCaptureVideoPreviewLayer! //MARK: Out
var captureSession: AVCaptureSession!
var photoOutput: AVCapturePhotoOutput!
var previewLayer : AVCaptureVideoPreviewLayer!
//MARK: Outlets
@IBOutlet weak var previewView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
captureSession = AVCaptureSession()
captureSession.sessionPreset = AVCaptureSessionPresetPhoto
//Ask permission to camera
let device = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .back)
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted: Bool) in
if granted {
print("granted")
//Set up session
if let input = try? AVCaptureDeviceInput(device: device) {
print("Input = device")
if (self.captureSession.canAddInput(input)) {
self.captureSession.addInput(input)
print("Input added to capture session")
if (self.captureSession.canAddOutput(self.photoOutput)) {
print("Output added to capture session")
self.captureSession.addOutput(self.photoOutput)
self.previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
self.previewLayer.frame = self.previewView.bounds
self.previewView.layer.addSublayer(self.previewLayer!)
self.captureSession.startRunning()
print("Session is running")
}
}
}
}
else {
print("Goodbye")
}
})
}
不幸的是,我只能让它打印出来,直到“输入添加到捕获会话”。任何建议都会有帮助-谢谢 您必须删除以前在会话中添加的输出。您可以使用for循环来实现这一点
用于captureSession.outputs中的输出{captureSession.removeOutput(输出)}
然后尝试添加新的输出您在哪里为
self.photoOutput
赋值?