2017-01-26 7 views
0

GCDの迅速なサポートが必要です。私はこのツールを使い始めました。ちょっと混乱します。だから私は、写真を撮ってvarに保存する関数foo()があるとしましょう。この関数の中には、その写真に顔があるかどうかをチェックして真か偽かをチェックする別の関数があります。ユーザーはこのようなその写真上の任意の顔がない時はいつでも、それはカスタムデリゲートを通じて写真が保存されますusePhotoFromPreviewボタンをタップすると次に、:iOSでの写真撮影を伴う同時画像処理タスク

var image: UIImage? 
var checkingPhotoForFaces: Bool? 

func foo(){ 
let videoConnection = imageOutput?.connection(withMediaType: AVMediaTypeVideo) 
    imageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (imageDataSampleBuffer, error) -> Void in 

     if let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer) { 
      image = UIImage(data: imageData) 

      self.checkingPhotoForFaces = self.detect(image: image) 
     } 

    }) 

} 

func detect(image: UIImage) -> Bool{ 
..... 
} 

func usePhotoFromPreview(){ 
    self.dismiss(animated: true, completion: { 
     if self.checkingPhotoForFaces == false{ 
      self.delegate?.takenPhoto(photo: self.image!) 
      print("photo taken") 
     }else{ 
      self.delegate?.takenPhoto(photo: nil) 
      print("no photo taken") 
     } 
    }) 
} 

だから今トリッキーな部分は、私がする機能を検出したいのですが非同期に実行され、完了したときにのみusePhotoFromPreviewを実行します。私はCoreImageの実装のためにメインスレッド上にあるはずのfuncを検出します。私は単にこのような何か作っ:

func foo(){ 
... 
DispatchQueue.global().async { 
       self.checkingPhotoForFaces = self.detect(image: stillImage) 
      } 
... 
} 

をしかし、問題は、ユーザーがそのボタンにあまりにも速いタップしたとき、それは動作しませんで、cuzのfuncがまだ処理された検出、ので、私はキューのいくつかの種類が必要だと思うけどどちらが混乱していますか?

btw。 、上記のいくつかのパラメータの不足に固執しないでください、私はその場でそれを書いて、それが

+0

あなたはそれが行われるまで待つことができるように '検出()'関数に完了閉鎖パラメータを追加する必要があります。 – shallowThought

+0

私はそれが顔の検出であると理解していますが、Core Imageに関連するものはGPU上で動作する可能性があります。もしそうなら、それは*メインスレッド*のコンセプトを放棄しないでしょうか? – dfd

+0

@shallowThought okですが、どうすれば使用できますか? –

答えて

0

変更は上記のコメントを検出

はあなたの助けをありがとうポイントではありません。

func detect(image: UIImage, completion:()->(detectedFaces: Bool)){ 
    //..... 

    completion(true|false) 
} 
その後

if let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer) { 
     image = UIImage(data: imageData) 

     self.detect(image) { detectedFaces in 
      self.checkingPhotoForFaces = detectedFaces 
      //Enable button? 
     } 
    } 
+0

ok、これは私に役立つ正しいcuzとマークします(私も同様です) –

関連する問題