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

标签 ios objective-c xcode sprite-kit avcapturesession

在游戏的主视图 Controller 的 View 中,我使用 AVCaptureVideoPreviewLayer 设置第一层(索引 0),在 View 上方我放置了具有透明背景的 SKView(不透明 = NO 且背景颜色 = 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?了解更多信息。

这有帮助吗?

关于ios - SpriteKit,如何将实时摄像机放置在后台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32947452/

相关文章:

objective-c - 调试statfs?

swift - AKMIDICallbackInstrument 实现问题

iphone - Xcode 5命令行:体系结构“arm64”的无效部署目标“3.0.0”

ios - Facebook API : Unknown fields: name?

objective-c - 使用 ShareKit 将 UILabel 传递给 Facebook

ios - iMessage 扩展 : Root Navigation Controller results in all delegate methods to not get called in my MSMessagesAppViewController

ios - 监听用户在 iOS Switch 上执行的状态更改

iphone - 在 2 个 ViewController 之间导航

ios - 创建自定义调出 View

ios - 如何在 swift 4 中比较两个 CGColor