Question

Good morning, I have a AVCaptureVideoPreviewLayer added to my view by the method startPreview in the class ViewController.

- (void) startPreview
{
    preview = [[CameraEngine engine] getPreviewLayer];
    [preview removeAllAnimations];

    preview.frame = self.imageView.bounds;
    [[preview connection] setVideoOrientation:AVCaptureVideoOrientationPortrait];

    [self.cameraView.layer addSublayer:preview];
    [self.imageView.layer addSublayer:preview];    
}

then I have the following method to add an icon to the view

- (void)addIcon:(NSNotification *)notification{

   UIView* iview;

   NSDictionary* userInfo2 = [notification userInfo];
   int originX = [userInfo2[@"originX"]intValue];
   int originY = [userInfo2[@"originY"]intValue];

   iview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mouse_ico.png"]];

   [iview setFrame:CGRectMake(0,0, iview.frame.size.width/4, iview.frame.size.height/4)];
   [iview setUserInteractionEnabled:NO];
   [self.view addSubview:iview];
   [self.view setNeedsDisplay];
}

If I call the method "addIcon" by the code

[self addIcon:(NSNotification*)nil];

from the method "startPreview it works properly. When I wait the event from another class the icon doesn't appear. I debugged the code and the event is correctly triggered, and the method addIcon is called. "self.view" has the same memory address when the addIcon is reached from startPreview and when is triggered by the NotificationCenter.

Thank you

Was it helpful?

Solution

Check that your addIcon method is always running in the main thread in your debugger. Updating ui elements on a background thread can produce the results you are seeing, and shouldn't be done that way.

Here is an example of how you could wrap your view update code so it will always run on the main thread.

dispatch_async(dispatch_get_main_queue(), ^{
        //put update view code in here
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top