如何将CameraView加到cocos视图下方?
在安卓平台时使用的是:Cocos2dxActivity中的FrameLayout进行AddView并将层级设置为0:
layout.addView(surfaceView, 0);
。也就是surfaceView做为FrameLayout的SubView。所以在iOS平台上想着也是这样,就用了[eaglview addSubview:camera.view];
发现显示不出来相机画面。后来咨询大佬之后使用如下方法解决:
(oc)1 2 3 4
| [eaglview.superview addSubview:camera.view];
[eaglview.superview bringSubviewToFront:eaglview];
|
再次感谢大佬!!!
完整打开相机显示预览如下:
(oc)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| + (void) CreateCamera{ NSLog(@"到iOS platform 了"); if(camera == nil){ camera = [[NJCamera alloc] initWithQuality:AVCaptureSessionPresetPhoto position:NJCameraPositionRear OutputType:NJCameraOutputTypPhoto]; } auto clearColor = [UIColor clearColor]; auto view = cocos2d::Director::getInstance()->getOpenGLView(); cocos2d::Director::getInstance()->setClearColor(cocos2d::Color4F(0,0,0,0)); auto eaglview = (CCEAGLView *) view->getEAGLView(); eaglview.backgroundColor=clearColor; [eaglview.superview addSubview:camera.view]; [eaglview.superview bringSubviewToFront:eaglview]; camera.view.frame = eaglview.frame; [camera start]; }
|
注:
NJCamera使用:https://github.com/nijiehaha/NJCamera