CALayerのアニメーションをコマ送りして、コマごとにキャプチャし、それを継ぎ足していって動画を生成したいと考えています。
そのために下記のようなコードを書きました。
framesはアニメーションのトータルのフレーム数で、frameTimeは一コマ当たりの時間(=1/60秒)です。
for i in 0...(frames - 1) { let animationGroup = animations.closingAnimation(animations.durationOfClosingAnimation) animationGroup.speed = 0 animationGroup.timeOffset = frameTime * i animationView.layer.add(animationGroup, forKey: "morph") 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! movieCreator.createSecond(image: cgImage) animationView.layer.removeAnimation(forKey: "moprh") }
このコードを実行すると、コマ送りの様子が画面に表示されません。
ためしに、for inを外してframeTime*40といった適当な数値を設定し、1コマだけ表示させると、アニメーションの途中の画像が表示されるのですが、for inで回すと表示されなくなってしまいます。
DispatchQueue.main.asyncで囲ってもダメでした。
CAAnimationがGPUで処理されているためでしょうか??
CATransaction.begin()、setCompletionBlock、commit()のセットも試してみたのですが、それもダメでした。
なにかいい方法はありますでしょうか。
どなたか教えて頂けると幸いです。
よろしくお願い致します。
あなたの回答
tips
プレビュー