找到你要的答案

Q:How can I set AVCapture preview layer as a background of a sprite kit?

Q:我怎么能把avcapture预览层作为一个精灵盒背景吗?

I am trying to draw lines into my SKScene and I can successfully draw it but I want that lines in front of AVCapturePreviewLayer but I am getting something like this :

I want all the line above my cameraPreviewLayer.

and this is my code for this:

import SpriteKit
import UIKit
import AVFoundation

class GameScene: SKScene {

let captureSession = AVCaptureSession()

var shapeCross:SKShapeNode!

var error: NSError?

override func didMoveToView(view: SKView) {

    let devices = AVCaptureDevice.devices().filter{ $0.hasMediaType(AVMediaTypeVideo) && $0.position == AVCaptureDevicePosition.Back }

    if let captureDevice = devices.first as? AVCaptureDevice  {

        captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &error))
        captureSession.sessionPreset = AVCaptureSessionPresetPhoto
        captureSession.startRunning()

        if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) {
            previewLayer.bounds = CGRectMake(0.0, 0.0, 100, 100)
            previewLayer.position = CGPointMake(view.bounds.midX, view.bounds.midY)
            previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
            previewLayer.opacity = 1
            view.scene?.backgroundColor = UIColor.clearColor()
            view.layer.addSublayer(previewLayer)
        }
    }

    let bezierPath = UIBezierPath(rect: CGRect(x: 0, y: view.scene!.frame.midY, width: view.scene!.frame.width, height: 1))
    bezierPath.appendPath( UIBezierPath(rect: CGRect(x: view.scene!.frame.midX, y: 0, width: 1, height: view.scene!.frame.height)) )

    shapeCross = SKShapeNode(path: bezierPath.CGPath)


    shapeCross.strokeColor = UIColor.whiteColor()

    self.addChild(shapeCross)

}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

}

override func update(currentTime: CFTimeInterval) {

    }
}

I want my previewLayer behind my SKScene after that I want to do something like this :

view.scene?.backgroundColor = UIColor.clearColor()

So I can see previewLayer too and I can see my lines too.

I try to set zPosition of views but its not working.

This is my sample project.

我想画线进入我的skscene我可以成功地画它,但我要前行的avcapturepreviewlayer但我得到这样的东西:

我希望所有的线在我的camerapreviewlayer。

这是我的代码:

import SpriteKit
import UIKit
import AVFoundation

class GameScene: SKScene {

let captureSession = AVCaptureSession()

var shapeCross:SKShapeNode!

var error: NSError?

override func didMoveToView(view: SKView) {

    let devices = AVCaptureDevice.devices().filter{ $0.hasMediaType(AVMediaTypeVideo) && $0.position == AVCaptureDevicePosition.Back }

    if let captureDevice = devices.first as? AVCaptureDevice  {

        captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &error))
        captureSession.sessionPreset = AVCaptureSessionPresetPhoto
        captureSession.startRunning()

        if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) {
            previewLayer.bounds = CGRectMake(0.0, 0.0, 100, 100)
            previewLayer.position = CGPointMake(view.bounds.midX, view.bounds.midY)
            previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
            previewLayer.opacity = 1
            view.scene?.backgroundColor = UIColor.clearColor()
            view.layer.addSublayer(previewLayer)
        }
    }

    let bezierPath = UIBezierPath(rect: CGRect(x: 0, y: view.scene!.frame.midY, width: view.scene!.frame.width, height: 1))
    bezierPath.appendPath( UIBezierPath(rect: CGRect(x: view.scene!.frame.midX, y: 0, width: 1, height: view.scene!.frame.height)) )

    shapeCross = SKShapeNode(path: bezierPath.CGPath)


    shapeCross.strokeColor = UIColor.whiteColor()

    self.addChild(shapeCross)

}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

}

override func update(currentTime: CFTimeInterval) {

    }
}

我希望我的previewlayer在我SKScene之后,我想做这样的事:

view.scene?.backgroundColor = UIColor.clearColor()

所以我可以看到previewlayer太,我可以看到我的线。

我试图把zposition意见但它不工作。

这是我的样本项目。

swift  skspritenode  skscene  zposition