128 lines
5.1 KiB
Raw Normal View History

<h1><img src="" alt="Gifu Logo" style="border-radius: 6px"></h1>
2014-09-07 21:48:09 +00:00
[![GitHub release](]( [![Travis](]( [![Carthage compatible](]( [![Join the chat at](]( ![Swift 3.0.x]( ![platforms](
2016-01-31 12:00:48 +00:00
Gifu adds protocol-based, performance-aware animated GIF support to UIKit, without forcing you to use a `UIImageView` subclass. (It's also a [prefecture in Japan](
2016-09-25 18:12:56 +00:00
**Swift 2.3** support is on the [swift2.3]( branch. **This branch will not be getting any future updates**.
## Install
2016-09-25 18:12:56 +00:00
### [Carthage](
2014-12-08 22:22:46 +00:00
2015-12-02 22:47:47 +00:00
- Add the following to your Cartfile: `github "kaishin/Gifu"`
- Then run `carthage update`
- Follow the current instructions in [Carthage's README][carthage-installation]
for up to date installation instructions.
2014-09-07 21:45:51 +00:00
2015-12-02 22:47:47 +00:00
### [CocoaPods](
2015-12-02 22:47:47 +00:00
- Add the following to your [Podfile]( `pod 'Gifu'`
- You will also need to make sure you're opting into using frameworks: `use_frameworks!`
- Then run `pod install` with CocoaPods 0.36 or newer.
2014-12-08 22:22:46 +00:00
## How It Works
`Gifu` does not rely on subclassing `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):
<img src="" width="300" />
## Usage
2014-09-07 21:45:51 +00:00
There are two options that should cover any situation:
2014-09-07 21:45:51 +00:00
- Use the built-in `GIFImageView` subclass.
- Make `UIImageView` or any of its subclasses conform to `GIFAnimatable`.
2014-09-07 22:48:47 +00:00
### GIFImageView
2014-09-11 21:26:38 +00:00
A subclass of `UIImageView` that conforms to `GIFAnimatable`. You can use this class as-is or subclass it for further customization (not recommended).
2015-12-02 22:32:02 +00:00
### GIFAnimatable
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.
class MyImageView: UIImageView, GIFAnimatable {
public lazy var animator: Animator? = {
return Animator(withDelegate: self)
override public func display(_ layer: CALayer) {
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.
### Examples
The simplest way to get started is initializing a `GIFAnimatable` class in code or in a storyboard, then calling `animate(:)` on it.
let imageView = GIFImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
imageView.animate(withGIFNamed: "mugen")
You can also prepare for the animation when the view loads and only start animating after a user interaction.
// In your view controller..
override func viewDidLoad() {
imageView.prepareForAnimation(withGIFNamed: "mugen")
@IBAction func toggleAnimation(_ sender: AnyObject) {
if imageView.isAnimatingGIF {
} else {
If you are using a `GIFAnimatable` class in a table or collection view, you can call the `prepareForReuse()` method in your cell subclass:
override func prepareForReuse() {
### Demo App
2014-11-04 10:28:43 +00:00
Clone or download the repository and open `Gifu.xcworkspace` to check out the demo app.
2014-09-07 22:48:47 +00:00
## Documentation
See the [full API documentation](
2014-09-07 22:48:47 +00:00
2014-09-07 22:36:05 +00:00
## Compatibility
2014-09-07 21:45:51 +00:00
- iOS 9.0+
- Swift 3.0
- Xcode 8.0
2014-09-10 19:44:28 +00:00
## License
2014-09-07 21:45:51 +00:00