当在背景线程上调用函数时,带有透明图像的sprite显示为白框

时间:2013-04-08 14:15:13

标签: iphone objective-c cocos2d-iphone

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

screen shot of output

- (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];
}

1 个答案:

答案 0 :(得分:1)

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

dispatch_async(dispatch_get_main_queue(), ^{
    // UI here
});