如何在Swift中使用叠加式CAShapeLayer录制视频?

问题描述

我有一个CAShapeLayer,它可以识别某人的脸。我遇到的问题是,当我完成录制并观看视频时,CAShapeLayer不会出现。为什么会发生这种情况,我将如何解决?谢谢!

 // Create a new layer drawing the bounding box
private func createLayer(in rect: CGRect) {


    let mask = CAShapeLayer()
    mask.frame = rect
    mask.cornerRadius = 10
    mask.opacity = 0.8
    mask.backgroundColor = UIColor.black.cgColor

    maskLayer.append(mask)
    layer.insertSublayer(mask,at: 1)

} 



 func drawFaceboundingBox(face : VNFaceObservation) {
    
    let transform = CGAffineTransform(scaleX: 1,y: -1).translatedBy(x: 0,y: -frame.height)
    
    let translate = CGAffineTransform.identity.scaledBy(x: frame.width,y: frame.height)
    
    let facebounds = face.boundingBox.applying(translate).applying(transform)

    createLayer(in: facebounds)
 }

//Start Recording
func startRecording() {

    if movieOutput.isRecording == false {
    
        let connection = movieOutput.connection(with: AVMediaType.video)
    
        if (connection?.isVideoOrientationSupported)! {
            connection?.videoOrientation = currentVideoOrientation()
        }
    
        if (connection?.isVideoStabilizationSupported)! {
            connection?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.auto
        }
    
        let device = activeInput.device
    
        if (device.isSmoothAutoFocusSupported) {
        
            do {
                try device.lockForConfiguration()
                device.isSmoothAutoFocusEnabled = false
                device.unlockForConfiguration()
            } catch {
               print("Error setting configuration: \(error)")
            }
        
        }
    
        outputURL = tempURL()
        movieOutput.startRecording(to: outputURL,recordingDelegate: self)
    
        }
        else {
            stopRecording()
        }

   }


//Will play video after recording video

class PlayBackRecordedVideo: UIViewController {

let avPlayer = AVPlayer()
var avPlayerLayer: AVPlayerLayer!
var videoURL: URL!

@IBOutlet weak var videoPlayBack: UIView!



override func viewDidLoad() {
    super.viewDidLoad()

        avPlayerLayer = AVPlayerLayer(player: avPlayer)
        avPlayerLayer.frame = view.bounds
        avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
        videoPlayBack.layer.insertSublayer(avPlayerLayer,at: 0)
    
        view.layoutIfNeeded()
    
        let playerItem = AVPlayerItem(url: videoURL as URL)
        avPlayer.replaceCurrentItem(with: playerItem)
        print("This is \(videoURL)")
    
        avPlayer.play()
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...