首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要等待setFocusModeLockedWithLensPosition信号量的完成吗?原子?NSCondition?

需要等待setFocusModeLockedWithLensPosition信号量的完成吗?原子?NSCondition?
EN

Stack Overflow用户
提问于 2017-08-12 03:33:06
回答 1查看 102关注 0票数 0

我没有太多使用信号量的经验,也没有使用块的经验。我已经看到了关于如何将异步调用转换为同步调用的各种建议。在这种情况下,在拍摄另一张照片之前,我只想等待确定iPhone的镜头已经改变了焦点。我添加了一个完成块(使用一个小例程来证明我看到了它)。但是如何阻塞我的其余代码(在主线程上运行),直到我得到完成回调?

代码语言:javascript
复制
- (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将其重置。但细节超出了我的能力。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-08-16 02:19:55

我自己解决了这个问题,一点也不难,而且看起来还不错。这是代码,以防它对其他人有帮助。如果你碰巧发现了一个错误,请告诉我。

代码语言:javascript
复制
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);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45642585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档