前提・実現したいこと
ここを参考にし、
カメラ動画から画像を取得してその画像をUIImageViewに表示したいです。
発生している問題・エラーメッセージ
AVCaptureVideoDataOutputSampleBufferDelegateのコールバックcaptureOutputメソッドが呼ばれません。
該当のソースコード
swift
1class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate { 2 3 @IBOutlet var previewView: UIImageView! 4 5 var session = AVCaptureSession() 6 var videoOutput = AVCaptureVideoDataOutput() 7 8 var images:[UIImage] = [] 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 // Do any additional setup after loading the view, typically from a nib. 13 14 if session.isRunning { 15 return 16 } 17 18 // セッションのインスタンス生成 19 let captureSession = AVCaptureSession() 20 21 // 入力(背面カメラ) 22 let videoDevice = AVCaptureDevice.default( 23 AVCaptureDevice.DeviceType.builtInWideAngleCamera, 24 for: AVMediaType.video, 25 position: AVCaptureDevice.Position.back 26 ) 27 videoDevice?.activeVideoMinFrameDuration = CMTimeMake(1, 30)// フレームレート 1/30秒 28 let videoInput = try! AVCaptureDeviceInput.init(device: videoDevice!) 29 captureSession.addInput(videoInput) 30 // 出力(ビデオデータ) 31 videoOutput = AVCaptureVideoDataOutput() 32 33 // ピクセルフォーマット(32bit BGRA) 34 videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable : Int(kCVPixelFormatType_32BGRA)] as! [String : Any] 35 // キューのブロック中に新しいフレームが来たら削除する 36 videoOutput.alwaysDiscardsLateVideoFrames = true 37 // フレームをキャプチャするためのキューを指定 38 let queue:DispatchQueue = DispatchQueue(label: "myqueue", attributes: .concurrent) 39 videoOutput.setSampleBufferDelegate(self, queue: queue) 40 41 captureSession.addOutput(videoOutput) 42 captureSession.sessionPreset = AVCaptureSession.Preset.medium 43 44 // セッションの開始 45 DispatchQueue.global(qos: .userInitiated).async { 46 captureSession.startRunning() 47 } 48 } 49 50 func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) { 51 DispatchQueue.main.sync(execute: { 52 53 // imageFromSampleBuffer内で 54 let image = imageFromSampleBuffer(sampleBuffer: sampleBuffer) // ここにブレークポイントを設定しても実行されていない 55 56 // 取得画像を表示 57 previewView.image = image 58 }) 59 } 60}
補足情報(FW/ツールのバージョンなど)
xcode9.4.1 + swift3.0
回答1件
あなたの回答
tips
プレビュー