2017-06-14 3 views
1

私はカスタムカメラビューを統合し、その間に若干古くなったコードに従っています。 「カスタムカメラビューの統合AVCaptureDevice

:私はいくつかのエラーがあったが、私は彼らがここで

2.これまでのところ、現在のコードであるバー修正したと信じられてきました:

import Foundation 
import AVFoundation 
import UIKit 

class setupView : UIViewController { 

@IBOutlet var cameraView: UIView! 
@IBOutlet var nameTextField: UITextField! 

var captureSession = AVCaptureSession() 
var stillImageOutput = AVCapturePhotoOutput() 
var previewLayer = AVCaptureVideoPreviewLayer() 

override func viewDidLoad() { 

     let session = AVCaptureDeviceDiscoverySession.init(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .back) 
    if let device = session?.devices[0] { 

     if device.position == AVCaptureDevicePosition.back { 

      do { 

       let input = try AVCaptureDeviceInput(device: device) 
       if captureSession.canAddInput(input){ 
        captureSession.addInput(input) 
        stillImageOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG] 

        if captureSession.canAddOutput(stillImageOutput) { 
         captureSession.addOutput(stillImageOutput) 
         captureSession.startRunning() 

         previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
         previewLayer.AVLayerVideoGravityResizeAspectFill 
         previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait 
         cameraView.layer.addSublayer(previewLayer) 

         previewLayer.bounds = cameraView.frame 
         previewLayer.position = CGPoint(x: cameraView.frame.width/2, y:cameraView.frame.height/2) 

        } 

       } 

      }    catch { 



      } 
     } 
    } 

} 

@IBAction func takePhoto(_ sender: Any) { 
    } 

@IBAction func submitAction(_ sender: Any) { 

} 
} 

私は現在、2つのエラーを取得していますタイプAVCapturePhotoOutputの値がoutputSettings 『

「タイプ『AVCaptureVideoPreviewLayer』の値が何のメンバーを持っていない 『AVLayerVideoGravityResizeAspectFill』

」何のメンバーを持っていません』
+0

エラー「タイプAVCapturePhotoOutputの価値」のための非常に明確な説明します私はAVCaptureStillImageOutputに戻すと削除されますが、これは減価償却されています。 –

答えて

0
import AVFoundation 
    import Foundation 
    @IBOutlet weak var mainimage: UIImageView! 
    let captureSession = AVCaptureSession() 
    let stillImageOutput = AVCaptureStillImageOutput() 
    var previewLayer : AVCaptureVideoPreviewLayer? 
    var captureDevice : AVCaptureDevice? 
     override func viewDidLoad() { 
     super.viewDidLoad() 
      captureSession.sessionPreset = AVCaptureSessionPresetHigh 

      if let devices = AVCaptureDevice.devices() as? [AVCaptureDevice] { 
       // Loop through all the capture devices on this phone 
       for device in devices { 
        // Make sure this particular device supports video 
        if (device.hasMediaType(AVMediaTypeVideo)) { 
         // Finally check the position and confirm we've got the back camera 
         if(device.position == AVCaptureDevicePosition.front) { 
          captureDevice = device 
          if captureDevice != nil { 
           print("Capture device found") 
           beginSession() 
          } 
         } 
        } 
       } 
      } 


    } 



func beginSession() { 

     do { 
      try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice)) 
      stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG] 

      if captureSession.canAddOutput(stillImageOutput) { 
       captureSession.addOutput(stillImageOutput) 
      } 

     } 
     catch { 
      print("error: \(error.localizedDescription)") 
     } 

     guard let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) else { 
      print("no preview layer") 
      return 
     } 

     self.view.layer.addSublayer(previewLayer) 
     previewLayer.frame = self.view.layer.frame 
     captureSession.startRunning() 
     self.view.addSubview(mainimage) 

    } 

このコードは私のアプリで働いている

1

あなたは、ほとんどがあります。問題はAVFoundationクラスのいくつかが廃止され、現在写真を撮る方法が複数あることです。ここであなたのコードの問題です。

「タイプAVCapturePhotoOutputの値は、」実際にAVCapturePhotoOutputoutputSettingsとして定義された任意のメンバーを持っていないので、それは何の部材「outputSettings」

を持っていません。実際AVCapturePhotoOutput

outputSettingsのチェックアウトフルdocumentationAVCaptureStillImageOutputのメンバーであり、同10.0

「バリュータイプの "AVCaptureVideoPreviewLayerのiOSから廃止されましたAVLayerVideoGravityResizeAspectFill "

" 何のメンバー を持っていません"繰り返しますが、コードごとに、AVCaptureVideoPreviewLayerのメンバーはありません。ビデオプレビューレイヤーを設定する場合は、以下のように設定してください。

previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 

あなたは、コードを述べなどが古くなっていると、本当に、あなたは、以下の手順に従ってくださいAVCapturePhotoOutputを使用する場合は、そのはAVCaptureStillImageOutput

を非推奨使用して。

これは写真を撮影する手順です。

  • AVCapturePhotoOutputオブジェクトを作成します。そのプロパティを使用して、サポートされているキャプチャ設定を決定し、特定の機能(ライブフォトをキャプチャするかどうかなど)を有効にします。
  • AVCapturePhotoSettingsオブジェクトを作成および設定して、特定のキャプチャの機能および設定を選択します(たとえば、イメージ安定化またはフラッシュを有効にするかどうかなど)。
  • 写真設定オブジェクトをcapturePhoto(with:delegate :)メソッドに渡し、AVCapturePhotoCaptureDelegateプロトコルを実装するデリゲートオブジェクトと共に画像をキャプチャします。その後、キャプチャプロセス中に重要なイベントを通知するために、フォトキャプチャ出力から代理人がコールされます。

あなたのclickCaptureメソッドでこれを以下のコードにして、あなたのクラスで委任することを確認し実装するのを忘れないでください。あなたがチェックアウトavfoundationから写真を取り込むには別の方法をお知りになりたい場合は

let settings = AVCapturePhotoSettings() 
let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first! 
let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType, 
          kCVPixelBufferWidthKey as String: 160, 
          kCVPixelBufferHeightKey as String: 160, 
          ] 
settings.previewPhotoFormat = previewFormat 
self.cameraOutput.capturePhoto(with: settings, delegate: self) 

は私の前にはSO answer

はまた、Apple社のドキュメントには、How to use AVCapturePhotoOutput

関連する問題