All Projects → kaishin → Gifu

kaishin / Gifu

Licence: other
High-performance animated GIF support for iOS in Swift

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Gifu

Sdwebimageflplugin
A SDWebImage plugin to support GIF using FLAnimatedImage and category
Stars: ✭ 16 (-99.41%)
Mutual labels:  gif, cocoapods, carthage
Sdwebimage
Asynchronous image downloader with cache support as a UIImageView category
Stars: ✭ 23,928 (+785.24%)
Mutual labels:  gif, cocoapods, carthage
Fusuma
Instagram-like photo browser and a camera feature with a few line of code in Swift.
Stars: ✭ 2,434 (-9.95%)
Mutual labels:  cocoapods, carthage
Imagetransition
Library for smooth animation of images during transitions.
Stars: ✭ 195 (-92.79%)
Mutual labels:  cocoapods, carthage
Tutti
Tutti is a Swift library that lets you create tutorials, hints and onboarding experiences.
Stars: ✭ 224 (-91.71%)
Mutual labels:  cocoapods, carthage
Amplitude Ios
Native iOS/tvOS/macOS SDK
Stars: ✭ 216 (-92.01%)
Mutual labels:  cocoapods, carthage
Whatsnewkit
Showcase your awesome new app features 📱
Stars: ✭ 2,329 (-13.84%)
Mutual labels:  cocoapods, carthage
Aksidemenu
Beautiful iOS side menu library with parallax effect. Written in Swift
Stars: ✭ 216 (-92.01%)
Mutual labels:  cocoapods, carthage
Icimulator
Simulate camera functions on iOS Simulator with images, videos, or your MacBook Camera.
Stars: ✭ 177 (-93.45%)
Mutual labels:  cocoapods, carthage
Swiftdailyapi
A Swift API framework for ZhiHu's Daily News.
Stars: ✭ 204 (-92.45%)
Mutual labels:  cocoapods, carthage
Tkradarchart
A customizable radar chart in Swift
Stars: ✭ 199 (-92.64%)
Mutual labels:  cocoapods, carthage
Shsearchbar
The search bar that doesn't suck.
Stars: ✭ 206 (-92.38%)
Mutual labels:  cocoapods, carthage
Admozaiccollectionviewlayout
ADMozaicCollectionViewLayout is yet another UICollectionViewLayout subclass that implements "brick", "mozaic" or Pinterest style layout.
Stars: ✭ 226 (-91.64%)
Mutual labels:  cocoapods, carthage
Hxphotopicker
图片/视频选择器 - 支持LivePhoto、GIF图片选择、3DTouch预览、在线下载iCloud上的资源、编辑图片/视频、浏览网络图片 功能 Imitation wx photo/image picker - support for LivePhoto, GIF image selection, 3DTouch preview, Download the resources on iCloud online, browse the web image function
Stars: ✭ 2,363 (-12.58%)
Mutual labels:  gif, cocoapods
Swiftyanimate
Composable animations in Swift
Stars: ✭ 194 (-92.82%)
Mutual labels:  cocoapods, carthage
L10n Swift
Localization of the application with ability to change language "on the fly" and support for plural form in any language.
Stars: ✭ 177 (-93.45%)
Mutual labels:  cocoapods, carthage
Pincache
Fast, non-deadlocking parallel object cache for iOS, tvOS and OS X
Stars: ✭ 2,513 (-7.03%)
Mutual labels:  cocoapods, carthage
Theanimation
Type-safe CAAnimation wrapper. It makes preventing to set wrong type values.
Stars: ✭ 214 (-92.08%)
Mutual labels:  cocoapods, carthage
Wkcookiewebview
WKWebView with cookie sharing support
Stars: ✭ 171 (-93.67%)
Mutual labels:  cocoapods, carthage
Irldocumentscanner
A drop-in Objective-C ViewController that will Automatically scan a document for you you.
Stars: ✭ 172 (-93.64%)
Mutual labels:  cocoapods, carthage

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].