0

下面的方法来自《 Professional iOS Augmented Reality book (Apress) 》一书的github存储库。

在基于整体的示例中没有任何代码UIViewController处理清理。此处调用的 CoreImage 人脸检测例程可能需要几秒钟才能完成。

如果用户导致这个 viewController 消失了怎么办?我知道 ^block 由队列保留,这是将消息发送到 nil (当人脸检测例程返回时)实际上是一种好处的情况吗?

- (IBAction)detectFacialFeatures:(id)sender {

    self.detectingView.hidden = NO;
    self.scrollView.scrollEnabled = NO;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        CIImage *image = [[CIImage alloc] initWithImage:[FACE_IMAGES objectAtIndex:self.currentIndex]];

        NSString *accuracy = self.useHighAccuracy ? CIDetectorAccuracyHigh : CIDetectorAccuracyLow;
        NSDictionary *options = [NSDictionary dictionaryWithObject:accuracy forKey:CIDetectorAccuracy];
        CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:options];

        NSArray *features = [detector featuresInImage:image];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self drawImageAnnotatedWithFeatures:features];
        });    
    });
}
4

1 回答 1

2

dispatch_async()复制该块,因为它将使其超出其声明范围。块中引用的所有对象在复制时都会保留。因此,假设您所指的 viewController 是self,它不会是nil。它将在块的生命周期内保留。

于 2012-04-29T19:55:34.257 回答