项目作者: SDWebImage

项目描述 :
A Lottie animation coder which use SDAnimatedImageView instead of LOTAnimationView for bitmap rendering
高级语言: Objective-C
项目地址: git://github.com/SDWebImage/SDWebImageLottieCoder.git
创建时间: 2020-03-08T11:24:46Z
项目社区:https://github.com/SDWebImage/SDWebImageLottieCoder

开源协议:MIT License

下载


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.

  • Pros: It use vector rendering technology like Core Animation Layer, which means you can change your view dynamic size without lossing details or regenerate images.

  • Cons: Vector rendering is much slower than bitmap rendering. For small and massive lottie images, like emojis, small icons, this is not suitable.

SDWebImageLottieCoder

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

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

  • Pros: It use bitmap rendering, each animation frame are rendered into the rasterized bitmap, not vector images. You can also preload all frames into memory, to get the best performance and 60FPS. This is also easy to integrate to UIKit/AppKit native framework.

  • Cons: Bitmap rendering does not support dynamic size changes. Once you want larger images, you need re-decoding the source lottie JSON, which is time-consuming and RAM consuming.

Example

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

Requirements

  • iOS 9+
  • macOS 10.11+
  • tvOS 9.0+
  • watchOS 2.0+
  • Xcode 11+

Installation

CocoaPods

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

  1. pod 'SDWebImageLottieCoder'

Carthage

SDWebImageLottieCoder is available through Carthage.

  1. github "SDWebImage/SDWebImageLottieCoder"

Swift Package Manager (Xcode 11+)

SDWebImageLottieCoder is available through Swift Package Manager.

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

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

  1. 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).

  • Objective-C
  1. // Add coder
  2. SDImageLottieCoder *lottieCoder = [SDImageLottieCoder sharedCoder];
  3. [[SDImageCodersManager sharedManager] addCoder:lottieCoder];
  • Swift
  1. // Add coder
  2. let lottieCoder = SDImageLottieCoder.shared
  3. SDImageCodersManager.shared.addCoder(lottieCoder)

Loading

  • Objective-C
  1. // Lottie json loading
  2. NSURL *lottieURL;
  3. UIImageView *imageView;
  4. [imageView sd_setImageWithURL:lottieURL];
  • Swift
  1. // Lottie json loading
  2. let lottieURL: URL
  3. let imageView: UIImageView
  4. imageView.sd_setImage(with: lottieURL)

Animation and Size

  • Objective-C
  1. // Lottie json loading on animated image view
  2. NSURL *lottieURL;
  3. SDAnimatedImageView *imageView;
  4. CGSize pixelSize = CGSizeMake(300, 300);
  5. [imageView sd_setImageWithURL:lottieURL placeholderImage:nil options:0 context:@{SDWebImageThumbnailPixelSize:@(pixelSize)}];
  • Swift
  1. // Lottie json loading on animated image view
  2. let lottieURL: URL
  3. let imageView: SDAnimatedImageView
  4. let pixelSize = CGSize(width: 300, height: 300)
  5. 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.

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

Screenshot

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