如何在Sprite Kit Swift 3中将AVCaptureVideoPreviewLayer设置为SKScene中的背景

时间:2017-02-15 20:35:41

标签: ios swift swift3 avfoundation augmented-reality

所以我试图在swift 3中使用Sprite工具包构建一个增强现实游戏。我能够将我的相机的视频设置为背景但它出现在我的节点上。基本上,节点位于AVCaptureVideoPreviewLayer下面。如何让我的节点出现在背景上(类似于神奇宝贝go)?我甚至试图改变zPosition,但这不起作用。这是我的代码:

import SpriteKit
import AVFoundation
class GameScene: SKScene {
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
override func didMove(to view: SKView) {
    isPaused = false
    addPlayer()

    scene?.backgroundColor = .clear
    captureSession = AVCaptureSession()

    let videoCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
    let videoInput: AVCaptureDeviceInput
    do {
        videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
    } catch {
        return
    }
    captureSession.startRunning()
    if (captureSession.canAddInput(videoInput)) {
        captureSession.addInput(videoInput)
    } else {
        return
    }


    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    previewLayer.frame = view.layer.bounds
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
    view.layer.addSublayer(previewLayer)
    captureSession.startRunning() 
 }

0 个答案:

没有答案