swift4
1import UIKit 2import AVFoundation 3 4class ViewController: UIViewController { 5 6 let captureSession = AVCaptureSession() 7 var previewLayer:CALayer! 8 var captureDevice:AVCaptureDevice! 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 prepareCamera() 13 } 14 15 func prepareCamera() { 16 captureSession.sessionPreset = AVCaptureSession.Preset.photo 17 ① if let availabelDevices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back).devices { 18 captureDevice = availabelDevices.first 19 beginSession() 20 } 21 } 22 23 func beginSession () { 24 do { 25 let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice) 26 captureSession.addInput(captureDeviceInput) 27 }catch { 28 print(error.localizedDescription) 29 } 30 ② if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) { 31 self.previewLayer = previewLayer 32 self.view.layer.addSublayer(self.previewLayer) 33 self.previewLayer.frame = self.view.layer.frame 34 captureSession.startRunning() 35 let dataOutput = AVCaptureVideoDataOutput() 36 dataOutput.videoSettings = [((kCVPixelBufferPixelFormatTypeKey as NSString) as String):NSNumber(value:kCVPixelFormatType_48RGB)] 37 dataOutput.alwaysDiscardsLateVideoFrames = true 38 if captureSession.canAddOutput(dataOutput) { 39 captureSession.addOutput(dataOutput) 40 } 41 captureSession.commitConfiguration() 42 } 43 } 44 45 override func didReceiveMemoryWarning() { 46 super.didReceiveMemoryWarning() 47 } 48 49} 50 51二箇所エラーが表示されます。 52修正方法を教えて戴けませんか ? 53 54① if let availabelDevices - - - 箇所で 55 Initializer for conditional binding must have Optional type, not '[AVCaptureDevice]' 56 57② if let previewLayer - - - 箇所で 58 Initializer for conditional binding must have Optional type, not 'AVCaptureVideoPreviewLayer' 59 というエラーになります。
画像が正しく表示されていませんし、そもそもエラーメッセージはテキストで書いて下さい。
回答1件
あなたの回答
tips
プレビュー