问题描述
我正在尝试将AVCaptureVideoPreviewLayer CALayer添加为我的SKScene的背景。我可以将CALayer添加到场景中,但无论什么尝试订购CALayer始终是最重要的对象。
I'm attempting to add the AVCaptureVideoPreviewLayer CALayer as the background to my SKScene. I can add the CALayer to the scene, but no matter what attempts at ordering the CALayer is always the top most object.
在didMoveToView中,我有以下功能:
In the didMoveToView I have the function:
- (void) didMoveToView:(SKView *)view
{
[self addVideo];
}
调用addVideo函数:
Which calls the addVideo function:
-(void) addVideo {
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
captureSession.sessionPreset = AVCaptureSessionPresetHigh;
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
[captureSession addInput:videoIn];
AVCaptureVideoPreviewLayer *avLayer =
[AVCaptureVideoPreviewLayer layerWithSession:captureSession];
avLayer.frame = self.view.bounds;
CGSize landscapeSize;
landscapeSize.width = self.view.bounds.size.height;
landscapeSize.height = self.view.bounds.size.width;
CGRect landscapeRect;
landscapeRect.size = landscapeSize;
landscapeRect.origin = self.view.bounds.origin;
avLayer.frame = landscapeRect;
if(avLayer.connection.supportsVideoOrientation)
{
avLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
}
[self.scene.view.layer insertSublayer:avLayer atIndex:0];
[self.scene.view.layer setNeedsLayout];
[captureSession startRunning];
}
我尝试过多次将AVCaptureVideoPreviewLayer CALayer作为背景的方法。每次将一个节点添加到场景中它都在这个CALayer上面,有什么建议吗?
I've attempted multiple ways to get that AVCaptureVideoPreviewLayer CALayer as the background. Everytime a node is added to the scene it is above this CALayer, any advice?
尝试以下函数:
- (void) sendSublayerToBack:(CALayer *)layer
{
[layer removeFromSuperlayer];
[self.view.layer insertSublayer:layer atIndex:0];
}
但仍无法正常工作....
But still not working....
推荐答案
不要在SKView图层中显示图层 - 创建另一个UIView并将其放置在视图层次结构中,使其位于SKView后面。然后将视频图层添加到该UIView。
Don't present the layer in the SKView layer - create another UIView and position it in the view hierarchy so it is behind the SKView. Then add the video layer to that UIView.
你必须让SKView透明,虽然在这里我不确定这是否有效 - 如果它完全有效它适用于cocos2d:将opaque属性设置为NO并将backgroundColor属性更改为nil(如果尚未为nil)。这应该使SKView透明。
You will have to make the SKView transparent, though here I'm not sure if this works - if at all it works like it does for cocos2d: set the opaque property to NO and change the backgroundColor property to nil (if not already nil). This should make the SKView transparent.
如果这些都不起作用,你可以找到使用SKVideoNode的变通方法。
If none of this works maybe you can find a workaround using SKVideoNode instead.
这篇关于SpriteKit SKScene添加AVCaptureVideoPreviewLayer CALayer作为后台无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!