-2

I have an animation which consists of 250 frames. Each frame is 1080x1920 resolution and in PNG format. I need to take all these frames, animate them with CAKeyframeAnimation and render them on a video using AVFoundation tools.

The issue arises when I try to create a values array for CAKeyframeAnimation. Initializing 250 FullHD images causes quite a big memory spike, which system detects and decides to kill the app. I've tried playing with autoreleasepool but it doesn't seem to help at all (no wonder, because CAKeyframeAnimation needs to hold on those images).

The problem goes even further: I actually have 10 of such animations! And I need to quickly switch between them because the app allows to preview those animations dynamically (which is done via AVSynchronizedLayer) before exporting everything in a video file.

So my question is: how can I load a big array of UIImage instances without system killing the app because of a memory surge? Is there an asynchronous way of loading them?

rmaddy
  • 298,130
  • 40
  • 468
  • 517
xinatanil
  • 969
  • 1
  • 12
  • 22

1 Answers1

2

how can I load a big array of UIImage instances without system killing the app because of a memory surge?

You can't. What you're trying to do is wrong from the outset.

  • If the goal is to make a "sprite animation" by using keyframe animation to change a layer's content periodically, the animation should consist of a small number of small images.

  • If the goal is to make a video based on a succession of images, a keyframe animation is not how to do it in the first place.

matt
  • 447,615
  • 74
  • 748
  • 977
  • For example, see https://stackoverflow.com/questions/3741323/how-do-i-export-uiimage-array-as-a-movie. But do _not_ follow its advice to form a `[UIImage]`! Form an array of image _names_ and load just _one_ image at a time, as needed, using care not to cache the image as you load it. The ImageIO framework gives you the tools to do that. – matt Oct 20 '18 at 16:12
  • The goal is to make a "sprite animation" by using keyframe animation and render that animation real-time on a FullHD video (with an option to then export that video). Sacrificing animation duration by reducing frames amount or reducing image quality is not an option, so I guess I'll have to dig into some other options. Anyway, thank you for your help! – xinatanil Oct 20 '18 at 17:59