Ios7 SpriteKit SKScene添加AVCaptureVideoPreviewLayer CALayer作为背景不工作

Ios7 SpriteKit SKScene添加AVCaptureVideoPreviewLayer CALayer作为背景不工作,ios7,core-animation,sprite-kit,Ios7,Core Animation,Sprite Kit,我正在尝试将AVCaptureVideoPreviewLayer CALayer作为背景添加到我的场景中。我可以将CALayer添加到场景中,但无论尝试如何排序,CALayer始终是最顶层的对象 在didMoveToView中,我具有以下功能: - (void) didMoveToView:(SKView *)view { [self addVideo]; } -(void) addVideo { AVCaptureSession *captureSession = [[AVCa

我正在尝试将AVCaptureVideoPreviewLayer CALayer作为背景添加到我的场景中。我可以将CALayer添加到场景中,但无论尝试如何排序,CALayer始终是最顶层的对象

在didMoveToView中,我具有以下功能:

- (void) didMoveToView:(SKView *)view
{
    [self addVideo];

} 
-(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];
- (void) sendSublayerToBack:(CALayer *)layer
{
   [layer removeFromSuperlayer];
    [self.view.layer insertSublayer:layer atIndex:0];
}
它调用addVideo函数:

- (void) didMoveToView:(SKView *)view
{
    [self addVideo];

} 
-(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];
- (void) sendSublayerToBack:(CALayer *)layer
{
   [layer removeFromSuperlayer];
    [self.view.layer insertSublayer:layer atIndex:0];
}
}

我尝试了多种方法将AVCaptureVideoPreviewLayer CALayer作为背景。每次将节点添加到场景中时,它都位于该层之上,有什么建议吗

尝试了以下功能:

- (void) didMoveToView:(SKView *)view
{
    [self addVideo];

} 
-(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];
- (void) sendSublayerToBack:(CALayer *)layer
{
   [layer removeFromSuperlayer];
    [self.view.layer insertSublayer:layer atIndex:0];
}

但仍然不工作….

不要在SKView层中显示该层-创建另一个UIView并将其放置在视图层次结构中,使其位于SKView后面。然后将视频层添加到该视图中

您必须使SKView透明,尽管我不确定这是否有效-如果它与cocos2d一样有效:将不透明属性设置为否,并将backgroundColor属性更改为nil(如果尚未为nil)。这将使SKView透明


如果这些都不起作用,也许你可以用SKVideoNode来解决问题。

看来我必须使用SKVideoNode,但如何获得像AVCaptureVideoPreviewLayer这样的实时摄像机视频源呢。SKVideoNode需要一个AVPlayer或视频文件NSURL,例如,如何将其连接到MyDocuments目录中的视频文件。我尝试了AVPlayer,但您需要停止AVCaptureSession才能获取视频,这不符合要求。需要表现得像AVCaptureVideoPreviewLayer,有什么建议吗?可能会为视频预览提供一个常规的UIKit故事板,当这部分完成后,返回到sprite kit视图控制器道歉如果我的初始代码不够清晰,视频将作为场景的背景,因此,SKNodes和游戏将出现在摄像机视频提要的顶部。是否有连接AVPlayer到AVCaptureVideoPreviewLayer的方法?或者将AVPlayer设置为某个实时摄像机源?不知道,但如果这是一个“增强现实”应用程序,您将希望改用UIImagePickerController。以下是cocos2d应用程序的设置:不幸的是,SKView无法透明,这是当前的限制。您解决了此Halford吗?@vburojevic很抱歉响应太晚,如果您have@Halford你是怎么解决的?谢谢