如何解决如何在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 举报,一经查实,本站将立刻删除。