Jiffy 1.0.1

Jiffy 1.0.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Nov 2016
SPMSupports SPM

Maintained by Mitch Treece.



Jiffy 1.0.1

Jiffy

Easy animated images for Swift! Based on AImage.

Overview

Jiffy makes working with animated images (.gif / .apng) a breeze. While there are a lot of libraries out there that accomplish something similar, most of them are usually plagued with un-needed features and performance issues. Jiffy aims to be a small & simple animated image library with little-to-no performance overhead.

This library is named after the absurd way my co-worker tries to pronounce “Gif”. It’s not a jar of peanut-butter, it’s an animated image.

Installation

Manually

You can also manually add the source files to your project.

  1. Clone this git repo
  2. Add all the Swift files in the Jiffy/ subdirectory to your project
  3. Profit

Jiffy

At it’s core, Jiffy is just a set of extensions over UIImage & UIImageView. This makes working with animated images familiar and easy.

let imageData = ...
let animatedImage = UIImage(animatedImageData: imageData)

let imageView = UIImageView(animatedImage: animatedImage)
imageView.frame = CGRect(x: 0, y: 0, 200, height: 200)
view.addSubview(imageView)

That’s it. You now have a beautiful animated image looping in your view! If you want to stop your animated image, just call imageView.stopAnimatedImage(). Naturally, a call to imageView.playAnimatedImage() will start your animation again.

You can also tell an existing UIImageView to start playing an animated image. This is useful when you want to layout your views with Storyboards.

@IBOutlet weak var imageView: UIImageView!

...

let imageData = ...
let animatedImage = UIImage(animatedImageData: imageData)
imageView.animate(with: animatedImage)

Image Quality

Jiffy also provides a way to specify image quality. The AnimatedImageQuality enum is defined as follows:

public enum AnimatedImageQuality: Float {

    case full = 1.0
    case high = 0.8
    case medium = 0.5
    case low = 0.2

    public static func custom(quality: Float) -> AnimatedImageQuality {
        return AnimatedImageQuality(rawValue: quality)!
    }

}

Using this enum, we can pass a pre-defined quality level to UIImageView upon initialization with an animated image:

let animatedImage = UIImage(animatedImageData: imageData, quality: .high)

If one of the pre-defined quality levels doesn’t do it for you, you can pass a custom quality level:

let animatedImage = UIImage(animatedImageData: imageData, quality: AnimatedImageQuality.custom(quality: 0.42))

If no quality is passed to UIImageView, it will assume max (AnimatedImageQuality.full) quality settings.

Memory Limit

You can also specify a memory limit (in whole MB values) on UIImageView:

let imageView = UIImageView(animatedImage: animatedImage, memoryLimit: 5)

If no memory limit is passed to UIImageView, it will default to a 20MB limit.

Contributing

Pull-requests are more than welcome. I only ask that any additions made to this library stick to a simple & performant standard.