2017-01-03 10 views
-3

私はこのチュートリアルをyoutube:https://www.youtube.com/watch?v=uUTevJAhL3Qに従ったので、残りの部分をSwift 3に更新する方法を見つけることはできません。私はスウィフトで比較的新しいですが、誰かが私を助けてくれるのであれば、それは素晴らしいでしょう!スナップショットのカメラビューを再作成しようとしています。スウィフトSwift 2からSwift 3に償却されたコード

import UIKit 
import AVFoundation 

class CameraVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 


    var captureSession : AVCaptureSession? 
    var stillImageOutput : AVCaptureStillImageOutput? 
    var previewLayer : AVCaptureVideoPreviewLayer? 
    @IBOutlet var cameraView: UIView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


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

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 

     captureSession = AVCaptureSession() 
     captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080 

     var backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) 

     var error : NSError? 
     var input = AVCaptureDeviceInput(device: backCamera, error: &error) 

     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() 

      } 


     } 


    } 
    @IBOutlet var tempImageView: UIImageView! 


    func didPressTakePhoto(){ 

     if let videoConnection = stillImageOutput?.connection(withMediaType: AVMediaTypeVideo){ 
      videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait 
      stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { 
       (sampleBuffer, error) in 

       if sampleBuffer != nil { 


        var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) 
        var dataProvider = CGDataProvider(data: imageData as! CFData) 
        var cgImageRef = CGImage(jpegDataProviderSource: dataProvider, decode: nil, shouldInterpolate: true, intent: kCGRenderingIntentDefault) 

        var image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.Right) 

        self.tempImageView.image = image 
        self.tempImageView.isHidden = false 

       } 


      }) 
     } 


    } 


    var didTakePhoto = Bool() 

    func didPressTakeAnother(){ 
     if didTakePhoto == true{ 
      tempImageView.isHidden = true 
      didTakePhoto = false 

     } 
     else{ 
      captureSession?.startRunning() 
      didTakePhoto = true 
      didPressTakePhoto() 

     } 

    } 

    func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
     didPressTakeAnother() 
    } 

} 
+2

あなたの直面しているエラーとあなたが試みたものを正確に投稿できますか?誰もがこのコードを直接書き直して書き直すつもりはないと思う。 – Frankie

+0

これはvar stillImageOutputと関連しています:AVCaptureStillImageOutput?どのように影響を受けているのでしょうか? –

答えて

1

3はerror handlingの考え方を導入し、Appleの財団とコアのAPIのほとんど(すべてではない)が更新されてしまったので、代わりにinout &errorパラメータ方法throwsとエラーでエラーを捕捉し使用します。

あなたはこのように書くために使用

のでコード:スウィフト3では

var error : NSError? 
var input = AVCaptureDeviceInput(device: backCamera, error: &error) 

は、エラーパラメータをドロップするように更新され、新しいdotrycatchの構文を使用します。

do { 
    var input = try AVCaptureDeviceInput(device: backCamera) 
    //... input was assigned without an error, you can use in the scope of this statement 

} 
catch { 
    // an error occured attempting `AVCaptureDeviceInput(device: backCamera)` 
    print("an error occured") 
} 

好きな場合は、略記バージョン:

var input = try! AVCaptureDeviceInput(device: backCamera) 
// not safe: app will crash if AVCaptureDeviceInput fails 

var input = try? AVCaptureDeviceInput(device: backCamera) 
// safer: input will be assigned as `nil` if AVCaptureDeviceInput fails 

次回Swift 2からSwift 3に変換するときは、Xcodeの 'refactor'ツールを使ってみてください。これは自動的にこれらの変更を行うための非常に良い仕事です。

+0

ps:次にオーバーフローに関する質問をするときに、Xcodeがあなたに与えている実際のエラーを書くのを忘れないでください。 'AVCaptureDeviceInput ...型のイニシャライザでは呼び出せません。 – MathewS

+0

ありがとうございました! –

関連する問題