簡體   English   中英

清理^塊

[英]cleaning up ^blocks

下面的方法來自專業iOS增強現實書 (Apress)的github存儲庫。

基於UIViewController的整個示例中沒有任何代碼可以處理清理。 這里調用的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];
        });    
    });
}

dispatch_async()復制塊,因為它將使其超出其聲明范圍。 塊中引用的所有對象在復制時都會保留。 所以,假設你所引用的viewController是self ,它不會是nil 它將在塊的生命周期內保留。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM