2016-12-25 8 views
0

私はスイフトを使用しています。私はカスタムカメラを作ろうとしています。しかし、私が従うチュートリアルやリンゴの多くの文書にかかわらず、常に誤りがあります。このライン上の カスタムカメラを作ることができないようです。 "定数を初期化する前に使用"

class ViewController: UIViewController, UIImagePickerControllerDelegate { 

@IBOutlet var cameraView: UIView! 

var captureSession : AVCaptureSession? 
var stillImageOutput : AVCaptureStillImageOutput? 
var previewLayer : AVCaptureVideoPreviewLayer? 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    captureSession = AVCaptureSession() 
    captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080 

    let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) 
    let error : NSError? 

    do { 

     let input = try! AVCaptureDeviceInput (device: backCamera) 
     if (error == nil && captureSession?.canAddInput(input) != nil) { 

      captureSession?.addInput(input) 

      stillImageOutput = AVCaptureStillImageOutput() 
      stillImageOutput?.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG] 

      if (captureSession?.canAddOutput(stillImageOutput) != nil) { 
       captureSession?.addOutput(stillImageOutput) 

       previewLayer = AVCaptureVideoPreviewLayer (session: captureSession) 
       previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect 
       previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait 
       cameraView.layer.addSublayer(previewLayer!) 
       captureSession?.startRunning() 
      } 
     } 
    } catch { 

    } 
} 


override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    previewLayer?.frame = cameraView.bounds 
} 

if (error == nil && captureSession?.canAddInput(input) != nil) {は、一定の「エラー」の前に使用初期化されているというエラーがあります。私はこれを本当に理解していない。前もって感謝します。

答えて

0

エラーメッセージが表示されるので、初期化する前にerrorを使用しています。あなたはnilでそれを初期化することによってこの問題を解決することができます

let error: NSError? = nil 

またはより良い:あなたは、単にそれを削除することができますので、実際に

let error: Error? = nil 

は、あなたが、errorを使用しないでください:

... 
//let error : NSError? delete this line 

do { 
    ... 
    if (/* error == nil && delete this */captureSession?.canAddInput(input) != nil) { 
関連する問題