0

我正在检测面部特征并显示通过透明图像创建精灵的位置,如果我正在调用在主线程上添加精灵的函数精灵显示正确但是当我在后台线程或除主线程之外的任何其他线程上调用此函数时,它显示白框。下面我上传了屏幕截图和代码片段。

输出截图

- (id)init{

       if( (self=[super init])) {

      [self addBackImageAndControls];
  }
return self;
 }

-(void)addBackImageAndControls{

    UIImage *scalledImage =[originalImage1 scaleToSize:CGSizeMake(320, 480)];
    texture2D =[[CCTexture2D alloc] initWithImage:originalImage1];

    [self body_init];

    self.isTouchEnabled = YES;

    CCMenuItem *done = [CCMenuItemFont itemFromString:@"Done" block:^(id sender) {[self done];}];
    done.position = ccp(40, 60);
    CCMenu *starMenu = [CCMenu menuWithItems:done, nil];//
    starMenu.position =CGPointZero;
    [self addChild:starMenu z:0 tag:125];

    //[self callFunc:scalledImage];

   [NSThread detachNewThreadSelector:@selector(callFunc:) toTarget:self withObject:scalledImage];

}

-(void)done{

  md.LipRect =  [self lipRect];

  [self removeAllChildrenWithCleanup:YES];

   CCMenuItem *item1 = [CCMenuItemFont itemFromString:@"One" block:^(id sender)
      {[self sizeOne];}];
   item1.position = ccp(40, 40);

  CCMenuItem *item2 = [CCMenuItemFont itemFromString:@"Two" block:^(id sender)
    {[self sizeTwo];}];
  item2.position = ccp(110, 40);

  CCMenuItem *item3 = [CCMenuItemFont itemFromString:@"Three" block:^(id sender)
  {[self sizeThree];}];
  item3.position = ccp(180, 40);

  CCMenu *starMenu = [CCMenu menuWithItems:item1,item2,item3, nil];//
  starMenu.position =CGPointZero;
  [self addChild:starMenu];

}

 -(void)callFunc:(UIImage*)image{


   [self opencvFaceDetect:image];
   [self opencvEyeDetect:image];
   [self opencvNoseDetect:image];

   [self LipsEffect];
}
4

1 回答 1

1

如果它正在更新 UI,则必须在主线程上完成。像这样包装 UI 更新:

dispatch_async(dispatch_get_main_queue(), ^{
    // UI here
});
于 2013-04-08T14:44:04.310 回答