⚠ **Swift 2.3** support is on the [swift2.3](https://github.com/kaishin/Gifu/tree/swift2.3) branch. **This branch will not be getting any future updates**.
`Gifu` does not force you to use a specific subclass of `UIImageView`. The `Animator` class does the heavy-lifting, while the `GIFAnimatable` protocol exposes the functionality to the view classes that conform to it, using protocol extensions.
The `Animator` has a `FrameStore` that only keeps a limited number of frames in-memory, effectively creating a buffer for the animation without consuming all the available memory. This approach makes loading large GIFs a lot more resource-friendly.
The figure below summarizes how this works in practice. Given an image
containing 10 frames, Gifu will load the current frame (red), buffer the next two frames in this example (orange), and empty up all the other frames to free up memory (gray):
The bread and butter of Gifu. Through protocol extensions, `GIFAnimatable` exposes all the APIs of the library, and with very little boilerplate, any `UIImageView` subclass can conform to it.
~~~swift
class MyImageView: UIImageView, GIFAnimatable {
public lazy var animator: Animator? = {
return Animator(withDelegate: self)
}()
override public func display(_ layer: CALayer) {
updateImageIfNeeded()
}
}
~~~
That's it. Now `MyImageView` is fully GIF-compatible, and any of these methods can be called on it:
-`prepareForAnimation(withGIFNamed:)` and `prepareForAnimation(withGIFData:)` to prepare the animator property for animation.
-`startAnimatingGIF()` and `stopAnimatingGIF()` to control the animation.
-`animate(withGIFNamed:)` and `animate(withGIFData:)` to prepare for animation and start animating immediately.
-`frameCount`, `isAnimatingGIF`, and `activeFrame` to inspect the GIF view.
-`prepareForReuse()` to free up resources.
-`updateImageIfNeeded()` to update the image property if necessary.
This approach is especially powerful when you want to combine the functionality of different image libraries.
~~~swift
class MyImageView: OtherImageClass, GIFAnimatable {}
~~~
Keep in mind that you need to have control over the class implementing `GIFAnimatable` since you cannot add the stored `Animator` property in an extension.