SpriteKit,如何在后台放置实时摄像机

SpriteKit, how can place live video camera in the background

在主 viewcontroller 游戏的视图中,我用 AVCaptureVideoPreviewLayer 设置了第一层(索引 0),在视图上方我放置了透明背景的 SKView,(不透明 = NO 和 backgroundColor = clearColor) 但这不是正确的方法,我该怎么办?

// video capture
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
[session addInput:input];
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
previewLayer.opaque = YES;
AVCaptureConnection *previewLayerConnection = previewLayer.connection;
if ([previewLayerConnection isVideoOrientationSupported]) {
    // aggiunge lo sfondo live solo se supportato dal device
    [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight | AVCaptureVideoOrientationLandscapeLeft];
    previewLayer.frame = self.view.bounds;
    [self.view.layer insertSublayer:previewLayer atIndex:0];
    [session startRunning];
}

// Create and configure the scene.
GameScene *scene = [GameScene unarchiveFromFile:@"GameScene"];
scene.scaleMode = SKSceneScaleModeAspectFill;

// Present the scene
// @property (nonatomic, weak) IBOutlet SKView *skView;
self.skView.opaque = NO;
self.skView.backgroundColor = [UIColor clearColor];
[self.skView presentScene:scene];

尝试将 skView 的 allowsTransparency 属性 设置为 true:

skView.allowsTransparency = true

有关详细信息,请参阅 Making a SKScene's background transparent not working... is this a bug?

这有帮助吗?