首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何捕获CAAnimation

如何捕获CAAnimation
EN

Stack Overflow用户
提问于 2017-11-14 14:46:22
回答 1查看 235关注 0票数 0

我想在CALayer上捕获动画的每一帧来生成mp4电影文件。

所以,我写了下面的代码。

frames是动画中的总帧数,frameTime是1/ 60秒,UIImageView上已经添加了animationLayer

代码语言:javascript
复制
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.asycCAtransition.begin(), .setCompletionBlock, .commit(),但它们不起作用。

我应该怎么做才能得到我想要的东西?

EN

回答 1

Stack Overflow用户

发布于 2017-11-14 19:55:59

您不应该将视图层次结构绘制到图像上下文中。相反,您应该像这样呈现表示层:

代码语言:javascript
复制
if let presentationLayer = drawView.layer.presentation() {
    presentationLayer.render(in: context)
}

表示层表示当前绘制在屏幕上的层的内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47279287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档