Swiftpack.co - Package - kaishin/Gifu

Logo

Test GitHub release Carthage compatible Swift 5.0 platforms

Gifu adds protocol-based, performance-aware animated GIF support to UIKit. (It's also a prefecture in Japan).

Install

Swift Package Manager

Add the following to your Package.switft file:

let package = Package(
    dependencies: [
    .package(url: "https://github.com/kaishin/Gifu.git", from: "3.2.2")
    ],
)

Carthage

  • Add the following to your Cartfile: github "kaishin/Gifu"
  • Then run carthage update
  • Follow the current instructions in Carthage's README for up to date installation instructions.

CocoaPods

  • 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.

How It Works

Gifu does not require using the built-in GIFImageView subclass. 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):

Usage

There are two options that should cover any situation:

  • Use the built-in GIFImageView subclass if you don't need to combine GIF support with another image library.
  • If you need more flexibility and composability, make your class conform to GIFAnimatable. In practice, any UIView subclass would do, since you get most of the required properties for free. For best results, make your UIImageView subclass conform to GIFAnimatable to get access to other features such as intrinsic content size.

GIFAnimatable

The bread and butter of Gifu. Through protocol extensions, GIFAnimatable exposes all the APIs of the library, and with very little boilerplate, any class can conform to it.

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 has access to all these methods and properties:

  • 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.

Furthermore, you can make any class GIF-animatable, starting with UIView subclasses:

class CustomAnimatedView: UIView, GIFAnimatable {
  public lazy var animator: Animator? = {
    return Animator(withDelegate: self)
  }()

  override public func display(_ layer: CALayer) {
    updateImageIfNeeded()
  }
}

You can also make UIKit classes conform using associated objects may you wish:

import UIKit
import Gifu

extension UIImageView: GIFAnimatable {
  private struct AssociatedKeys {
    static var AnimatorKey = "gifu.animator.key"
  }

  override open func display(_ layer: CALayer) {
    updateImageIfNeeded()
  }

  public var animator: Animator? {
    get {
      guard let animator = objc_getAssociatedObject(self, &AssociatedKeys.AnimatorKey) as? Animator else {
        let animator = Animator(withDelegate: self)
        self.animator = animator
        return animator
      }

      return animator
    }

    set {
      objc_setAssociatedObject(self, &AssociatedKeys.AnimatorKey, newValue as Animator?, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }
  }
}

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") {
  print("It's animating!")
}

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() {
  super.viewDidLoad()
  imageView.prepareForAnimation(withGIFNamed: "mugen") {
    print("Ready to animate!")
  }
}

@IBAction func toggleAnimation(_ sender: AnyObject) {
  if imageView.isAnimatingGIF {
    imageView.stopAnimatingGIF()
  } else {
    imageView.startAnimatingGIF()
  }
}

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() {
  super.prepareForReuse()
  imageView.prepareForReuse()
}

Demo App

Clone or download the repository and open Gifu.xcworkspace to check out the demo app.

Documentation

See the full API documentation.

Compatibility

  • iOS 9.0+
  • Swift 4.0
  • Xcode 9.0

License

See LICENSE.

Github

link
Stars: 2377

Dependencies

Used By

Total: 0

Releases

Add SPM support - 2019-11-30 18:40:42

3.2.0 - 2018-09-28 10:45:28

3.1.0 - 2018-06-10 13:28:09

New

  • tvOS support. (Demo app still missing)

Fixed

  • Update project to Xcode 9.4
  • Fix overlapping accesses to image container (Michael Schneider)

v3.0.0 - 2018-01-01 21:37:31

  • Xcode 9 and Swift 4 support.
  • Add completion handler methods to all animation methods.
  • Support loading remote GIFs using a URL.

v2.0.1 - 2017-07-30 17:52:09

  • Fix Xcode 8.3 warnings

2.0 - 2017-01-05 00:25:20

  • Swift 3 and Xcode 8 support.
  • NEW: Loop control.
  • API rewritten from scratch to take advantage of new Swift features such as protocol extensions.
  • Fixed memory leaks due to retain recycles.
  • Better documentation and examples.

2.0.0-rc - 2016-10-06 22:04:13

  • Swift 3 and Xcode 8 support.
  • API rewritten from scratch to take advantage of new Swift features such as protocol extensions.
  • Fixed memory leaks due retain recycles.
  • Better documentation and examples.

Better Async Preloading - 2016-05-28 19:05:27

  • [Fixed] Fix crash when frames not preloaded in time. (@mbcharbonneau)

Swift 2.2 - 2016-05-28 18:53:00

  • Update for Swift 2.2 (@kaishin & @Dershowitz011)
  • [New] Add frameCount public property. (@storix)
  • [New] Add needsPrescaling public property. (@storix)
  • [Fixed] Pause the display link upon initialization. (@kaishin)
  • [Fixed] Use proxy object to prevent strong reference cycle. (@mr-seiler)
  • [Fixed] Conditionally invalidate displayLink. (@mr-seiler)
  • [Fixed] Restore frame preloading to a working state. (@kaishin)
  • [Removed] Runes dependency. (@kaishin)

Reuse-friendly - 2016-02-18 10:42:52

  • Add a prepareForReuse() function that stops the animation and releases the animator.

- 2016-01-20 12:31:36

Make test scheme to build only when testing

One Point Oh - 2015-12-02 22:48:52

First stable release.

v0.10.0 - 2015-06-05 15:27:49

Improve memory management. Remove retain cycles.

- 2015-06-02 15:31:59

Updated Runes

Swift 1.2 Compatibility - 2015-04-17 15:29:49

Update Gifu for Swift 1.2 support.

v0.8.1 - 2014-12-21 10:32:53

Fix a bug with missing intrinsic size support.

v0.8.0 - 2014-12-15 09:33:40

  • API Breaking Changes: You will have to instantiate an AnimatedImage before passing it to the UIImageView.
  • Preloading has been disabled in favor of resizing and storing all frames upfront. Keeping the image data in memory yielded worse results in most of the cases that were tested (credits go to @tonyd256).

v0.7.1 - 2014-12-10 20:57:51

Fix project structure.

v0.7 - 2014-12-08 22:25:00

Add framework support.

0.6.0 - 2014-11-10 22:04:50

Fix animation methods naming conflicts with UIKit.

0.5.0 - 2014-11-10 22:04:10

Make library and demo app run on Xcode 6.1