下面的方法来自一个github存储库,用于专业iOS增强现实书(Apress)。
在整个UIViewController-based示例中没有任何处理清理的代码。这里调用的CoreImage人脸检测例程可能需要几秒钟才能完成。
如果用户导致导致此viewController消失的更改,会发生什么情况?我知道^块是由队列保留的,在这种情况下,将消息发送给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];
});
});
}发布于 2012-04-29 19:55:34
dispatch_async()复制该块,因为它会将其保留在声明范围之外。在复制块时,将保留块中引用的所有对象。因此,假设您所指的viewController是self,则不会是nil。它将一直保留到整个街区的寿命。
https://stackoverflow.com/questions/10375149
复制相似问题