2017-08-11 7 views
0

私はセマフォやブロックに関して多くの経験がありませんでした。私は、非同期呼び出しを同期呼び出しに変換する方法について、さまざまな提案を見てきました。この場合、別の写真をスナップする前に、iPhoneのレンズがフォーカスを変更したかどうかを確認したいだけです。 補完ブロックを追加しました(私がそれを見ていることを証明するための少しのルーチン付き)。しかし、完了コールバックを取得するまで私のコードの残りの部分(メインスレッドで実行中)をブロックする方法はありますか?setFocusModeLockedWithLensPositionの完了時に待機する必要があります - セマフォ?原子? NSCondition?

- (void) changeFocusSettings 
{ 
    if ([SettingsController settings].useFocusSweep) 
    { 
     // increment the focus setting 
     float tmp = [SettingsController settings].fsLensPosition; 
     float fstmp =[[SettingsController settings] nextLensPosition: [SettingsController settings].fsLensPosition]; // get next lensposition 
     [SettingsController settings].fsLensPosition = fstmp ; 
     tmp = [SettingsController settings].fsLensPosition; 
     if ([self.captureDevice lockForConfiguration: nil] == YES) 
     { 
      __weak typeof(self) weakSelf = self; 
      [self.captureDevice setFocusModeLockedWithLensPosition:tmp 
               completionHandler:^(CMTime syncTime) { 
                NSLog(@"focus over..time = %f", CMTimeGetSeconds(syncTime)); 
                [weakSelf focusCompletionHandler : syncTime]; 
               }]; 
     } 
    } 
} 

- (bool) focusCompletionHandler : (CMTime)syncTime 
{ 
    NSLog(@"focus done, time = %f", CMTimeGetSeconds(syncTime)); 
    return true; 
} 

changeFocusSettingsは、別のルーチンから完全に呼び出されます。私はchangeFocusSettingsの中にちょうどセットされたセマフォーをイメージしてから、focuscompletionHandlerがそれをリセットします。しかし、詳細は私を超えています。
ありがとうございます。

答えて

0

私はそれを自分で処理しましたが、それは全く難しくなく、動作しているようです。他の人を助ける場合のコードは次のとおりです。エラーが発生した場合は、私に知らせてください。

dispatch_semaphore_t focusSemaphore; 
... 
- (bool) focusCompletionHandler : (CMTime)syncTime 
{ 
    dispatch_semaphore_signal(focusSemaphore); 
    return true; 
} 

- (void) changeFocusSettings 
{ 
    focusSemaphore = dispatch_semaphore_create(0); // create semaphone to wait for focuschange to complete 
    if ([SettingsController settings].useFocusSweep) 
    { 
     // increment the fsLensposition 
     float tmp = [SettingsController settings].fsLensPosition; 
     float fstmp =[[SettingsController settings] nextLensPosition: [SettingsController settings].fsLensPosition]; // get next lensposition 
     [SettingsController settings].fsLensPosition = fstmp ; 
     tmp = [SettingsController settings].fsLensPosition; 
     NSLog(@"focus setting = %f and = %f", tmp, fstmp); 
     if ([self.captureDevice lockForConfiguration: nil] == YES) 
     { 
      __weak typeof(self) weakSelf = self; 
      [self.captureDevice setFocusModeLockedWithLensPosition:tmp 
               completionHandler:^(CMTime syncTime) { 
                   [weakSelf focusCompletionHandler : syncTime]; 

                }]; 
      dispatch_semaphore_wait(focusSemaphore, DISPATCH_TIME_FOREVER); 
     } 
    } 
} 
関連する問題