我想在CALayer上捕获动画的每一帧来生成mp4电影文件。
所以,我写了下面的代码。
frames是动画中的总帧数,frameTime是1/ 60秒,UIImageView上已经添加了animationLayer。
for i in 0...(frames - 1) {
//add the animation on specified frame to the CALayer
animationGroup.speed = 0
animationGroup.timeOffset = frameTime * i
animationLayer.add(animationGroup, forKey: "morph")
//capture the frame of the animation.
UIGraphicsBeginImageContextWithOptions(drawView.bounds.size, true, 0.0)
drawView.drawHierarchy(in: drawView.bounds, afterScreenUpdates: true)
uiImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
uiImage = globalFunc.resizeUIImage(uiImage, scale, 400)
cgImage = uiImage.cgImage!
//add the captured image to movie file.
movieCreator.create(image: cgImage)
animationLayer.removeAnimation(forKey: "moprh")
}此代码不起作用。
没有显示每一帧的图像,并且创建了电影文件,但没有动画。
我在上面的代码中尝试了DispatchQueue.main.asyc和CAtransition.begin(), .setCompletionBlock, .commit(),但它们不起作用。
我应该怎么做才能得到我想要的东西?
发布于 2017-11-14 19:55:59
您不应该将视图层次结构绘制到图像上下文中。相反,您应该像这样呈现表示层:
if let presentationLayer = drawView.layer.presentation() {
presentationLayer.render(in: context)
}表示层表示当前绘制在屏幕上的层的内容。
https://stackoverflow.com/questions/47279287
复制相似问题