2017-03-13 26 views
0

私は、ユーザーがボタンを押してから0.1秒ごとに画像を取得するアプリケーションを作成しています。しかし、私は画像間の一貫性のある0.1秒の遅延を得るのに問題があります。captureStillImage特定の時刻に非同期で動作しない

これはcaptureStillImageAsynchronouslyが非同期クロージャであるためですか。 AVCapturePhotoOutputからcapturePhotoに切り替えるとこの問題は解決しますか?ここに私のコードの要約があります。

var stillImageOutput : AVCaptureStillImageOutput? 

// Button action. 
@IBAction func takeImage(_ sender: UIButton) {  
    //take 1 image every 0.1 seconds scheduled using a timer 
    Timer.scheduledTimer(timeInterval: 0.1, 
         target: self, 
         selector: #selector(calldidPressTakePhoto), 
         userInfo: nil, 
         repeats: true) 
} 

// Take image 
    func didPressTakePhoto(){ 

     if let videoConnection = stillImageOutput?.connection(withMediaType: AVMediaTypeVideo){ 

      videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait 

      //Capture image. everything in this closure is ASYNCHRONOUS? 
      stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { 
       (sampleBuffer, error) in 

       //Do something with the result image... 

      }) 
     } 
    } 
+0

私の推測では、あなたが示唆したように非同期の問題になると思います。非同期で実行すると、基本的にコードが実行されたときは気にしないで、バックグラウンドで実行するだけです。だから、一貫して時間間隔を取得することはありません。 –

+0

2番目の質問では、 'capturePhoto'はデリゲートメソッドを呼び出します。残念ながら、それはタイミングと一貫しません。 – cuomo456

答えて

0

私の最善の策は、.captureStillImageAsynchronouslyが非同期である一方、それはまだ、共有リソースにアクセスするため、非常に可能性が高いので、すぐにそれが実行に重なってしまうことを、それを呼び出してからあなたを防ぐ内部mutexを持っているということです。私はあなたのカメラが0.1秒の火災率に追いつくのに問題があると思います。

関連する問題