Objective c 目标C-在AVCaptureVideoPreviewLayer顶部的事件后添加子视图
早上好, 我有一个AVCaptureVideoPreviewLayer通过类ViewController中的startPreview方法添加到我的视图中Objective c 目标C-在AVCaptureVideoPreviewLayer顶部的事件后添加子视图,objective-c,nsnotificationcenter,addsubview,Objective C,Nsnotificationcenter,Addsubview,早上好, 我有一个AVCaptureVideoPreviewLayer通过类ViewController中的startPreview方法添加到我的视图中 - (void) startPreview { preview = [[CameraEngine engine] getPreviewLayer]; [preview removeAllAnimations]; preview.frame = self.imageView.bounds; [[preview c
- (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];
}
然后,我使用以下方法将图标添加到视图中
- (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];
}
如果我通过代码调用方法“addIcon”
[self addIcon:(NSNotification*)nil];
从方法“StartView”中,它可以正常工作。
当我等待来自另一个类的事件时,图标不会出现。
我调试了代码,事件被正确触发,并且调用了addIcon方法。
从StartView访问外接程序和由NotificationCenter触发时,“self.view”具有相同的内存地址
谢谢请检查您的
addIcon
方法是否始终在调试器的主线程中运行。在后台线程上更新ui元素可能会产生您看到的结果,不应该这样做
下面是一个示例,说明如何包装视图更新代码,使其始终在主线程上运行
dispatch_async(dispatch_get_main_queue(), ^{
//put update view code in here
});
addIcon是否始终在主线程中运行?