Home

Awesome

SDWebImageLottieCoder

CI Status Version License Platform SwiftPM compatible Carthage compatible

What's for

This is a coder plugin for Lottie Animation format.

Differences

We've already built one Lottie plugin, called SDWebImageLottiePlugin.

The main difference for these two components, it's How we play animation, and What dependency we use. In order to reduce the code size for unnecessary dependency, we separate these into 2 different repos.

SDWebImageLottiePlugin

This lottie framework dependent lottie-ios, which is maintained by Airbnb.

This plugin can only play animation by using their own LOTAnimationView.

SDWebImageLottieCoder

This lottie framework dependent rlottie, which is maintained by Samsung.

This plugin can play animation on both SDAnimatedImageView and UIImageView/NSImageView.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

CocoaPods

SDWebImageLottieCoder is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SDWebImageLottieCoder'

Carthage

SDWebImageLottieCoder is available through Carthage.

github "SDWebImage/SDWebImageLottieCoder"

Swift Package Manager (Xcode 11+)

SDWebImageLottieCoder is available through Swift Package Manager.

let package = Package(
    dependencies: [
        .package(url: "https://github.com/SDWebImage/SDWebImageLottieCoder.git", from: "0.1")
    ]
)

SDWebImageLottieCoder is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SDWebImageLottieCoder'

Usage

Add Coder

Before using SDWebImage to load Lottie json, you need to register the Lottie Coder to your coders manager. This step is recommended to be done after your App launch (like AppDelegate method).

// Add coder
SDImageLottieCoder *lottieCoder = [SDImageLottieCoder sharedCoder];
[[SDImageCodersManager sharedManager] addCoder:lottieCoder];
// Add coder
let lottieCoder = SDImageLottieCoder.shared
SDImageCodersManager.shared.addCoder(lottieCoder)

Loading

// Lottie json loading
NSURL *lottieURL;
UIImageView *imageView;
[imageView sd_setImageWithURL:lottieURL];
// Lottie json loading
let lottieURL: URL
let imageView: UIImageView
imageView.sd_setImage(with: lottieURL)

Animation and Size

// Lottie json loading on animated image view
NSURL *lottieURL;
SDAnimatedImageView *imageView;
CGSize pixelSize = CGSizeMake(300, 300);
[imageView sd_setImageWithURL:lottieURL placeholderImage:nil options:0 context:@{SDWebImageThumbnailPixelSize:@(pixelSize)}];
// Lottie json loading on animated image view
let lottieURL: URL
let imageView: SDAnimatedImageView
let pixelSize = CGSize(width: 300, height: 300)
imageView.sd_setImage(with: lottieURL, placeholderImage: nil, options: [], contrext: [.thumbnailPixelSize : pixelSize])

Decoding

You can decode lottie image into aniamted UIImage/NSImage as well. If the lottie images have referenced external image resource, you can specify it as well.

// Lottie image decoding
NSData *lottieJSONData;
NSBundle *imageBundle; // You can even download the external image from online to local path, then load the lottie animation
UIImage *image = [[SDImageLottieCoder sharedCoder] decodedImageWithData:lottieJSONData options:@{SDImageCoderDecodeLottieResourcePath : imageBundle.resourcePath}];
// Lottie image decoding
let lottieJSONData: Data
let imageBundle: Bundle // You can even download the external image from online to local path, then load the lottie animation
let image = SDImageWebPCoder.shared.decodedImage(with: lottieJSONData, options: [.lottieResourcePath : imageBundle.resourcePath])

Screenshot

<img src="https://raw.githubusercontent.com/SDWebImage/SDWebImageLottieCoder/master/Example/Screenshot/LottieDemo.gif" width="300" />

These Lottie animation stickers are from lottiefiles-telegram

Author

DreamPiggy, lizhuoli1126@126.com

License

SDWebImageLottieCoder is available under the MIT license. See the LICENSE file for more info.

Thanks