Giter Club home page Giter Club logo

uiimageviewanimatedgif's Introduction

UIImageView+AnimatedGIF

Apple quietly introduced an API for playing animated GIFs and APNGs to the ImageIO framework in iOS 13. This project provides a high-level wrapper around that API that allows you to set animated images on UIImageViews.

Installation

Add the UIImageView+AnimatedGIF.h and UIImageView+AnimatedGIF.m source files to your project.

Usage

Animated images are represented using the TJAnimatedImage object, you can create a TJAnimatedImage using data or a file URL as input.

This library adds a category on UIImageView that gives it an animatedImage property, setting this to an instance of TJAnimatedImage will play the animated image, setting it to nil will stop playback.

UIImageView *imageView = /* an image view */;

// Play animated image from NSData
NSData *animatedImageData = /* animated image data */;
TJAnimatedImage *animatedImage1 = [TJAnimatedImage animatedImageWithData:animatedImageData];
imageView.animatedImage = animatedImage1;

// Play animated image from a file URL
NSURL *animatedImageFileURL = /* animated image file URL */;
TJAnimatedImage *animatedImage2 = [TJAnimatedImage animatedImageWithURL:animatedImageFileURL];
imageView.animatedImage = animatedImage2;

// Stop playback
imageView.animatedImage = nil;

You can also use the TJAnimatedImageView class to ensure image and animatedImage are mutually exclusive, explained in the next section.

Caveats

animatedImage/image exclusivity

One caveat with the UIImageView category provided here is that setting image externally will not stop playback. For example

imageView.animatedImage = /* an animated image */;
imageView.image = /* a still image */;
// Problem: imageView will continue displaying animatedImage.

One workaround for this is to manually nil out animatedImage before setting image.

imageView.animatedImage = /* an animated image */;
imageView.animatedImage = nil;
imageView.image = /* a still image */;
// imageView will correctly display the image.

Fortunately, this project provides a method to do this automatically. If you call +tj_configureStillImageAnimatedImageMutualExclusivity then UIImageView will guarantee that its image and animatedImage are mutually exclusive with no additional work on your part. Note that this method swizzles out UIImageView's implementation of -setImage: internally, which may not be desirable in your project.

A different way to work around this without tj_configureStillImageAnimatedImageMutualExclusivity is to use the TJAnimatedImageView subclass of UIImageView. TJAnimatedImageView enforces that setting image and animatedImage are mutually exclusive.

TJAnimatedImageView *animatedImageView = [TJAnimatedImageView new];
animatedImageView.animatedImage = /* an animated image */;
animatedImageView.image = /* a still image */;
// imageView will correctly display the image, animatedImage will be nilled out when image is set.

Other caveats

  • On iOS versions prior to 13 using this library just sets still images on image views. There is no animated fallback behavior. If you're looking for a library that supports older iOS versions I suggest FLAnimatedImage.
  • You must build with Xcode 11+ in order for this to build.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.