An engine to display image frames in animation without causing high-memory usage.
- Only the displaying image, rather than all of image frames, would occupy the memory.
- Supports image decode in background thread.
- Implemented by CADisplayLink.
import "CALayer+FXAnimationEngine.h" in your file first.
// load all image frames
NSArray<UIImage *> *frames = ...;
FXKeyframeAnimation *animation = [FXKeyframeAnimation animationWithIdentifier:@"xxx"];
animation.delegate = self;
animation.frameImages = frames;
animation.duration = 5.5;
animation.repeats = 3;
// decode image asynchronously
[xxxView.layer fx_playAnimationAsyncDecodeImage:animation];
FXKeyframeAnimation *animation = [FXKeyframeAnimation animation];
animation.count = 50; // [0, 49]
animation.duration = 4.2;
FXKeyframeAnimation *animation2 = [FXKeyframeAnimation animation];
animation2.count = 30; // [50, 79]
animation2.duration = 1.5;
animation2.repeats = 6; // repeat image between index 50 to index 79 six times
FXKeyframeAnimation *animation3 = [FXKeyframeAnimation animation];
animation.count = 20; // [80, 99]
animation.duration = 2;
FXAnimationGroup *animationGroup = [FXAnimationGroup animationWithIdentifier:@"xxxAnimation"];
animationGroup.animations = @[animation, animation2, animation3];
animationGroup.frames = frames;
animationGroup.delegate = self;
[xxxView.layer fx_playAnimation:animation];
- (void)fxAnimationDidStart:(FXAnimation *)anim {
// identify your animation by its "identifier" property
}
- (void)fxAnimationDidStop:(FXAnimation *)anim finished:(BOOL)finished {
// ...
}
For example, now you have 100 keyframe images and each one image is 160px * 320px. If every frame is 32-bit PNG, then one pixel will occupy 32 / (8 * 1024) KB. So one image will use 160 * 320 * 32 / (8 * 1024) KB memory, that is 200KB.
If you create your keyframe animation by Core Animation, all images will be loaded into memory(200KB * 100 / 1024 MB).
With FXAnimationEngine, creating a keyframe animation won't cause high-memory waterline, since only one image(200KB) occupies memory during animation.
This demo implements a living room using custom animation configuration to create FXKeyframeAnimation or CAKeyframeAnimation.
-
Add these lines below to your Podfile
platform :ios, 'xxx' target 'xxx' do pod 'FXAnimationEngine', '~> 1.0.0' end
-
Install the pod by running
pod install
Drag FXAnimationEngine
document with all files in it to your project
FXAnimationEngine is provided under the MIT license. See LICENSE file for details.