Swiftpack.co -  SDWebImage/SDWebImageLottieCoder as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
SDWebImage/SDWebImageLottieCoder
A Lottie animation coder which use SDAnimatedImageView instead of LOTAnimationView for bitmap rendering
.package(url: "https://github.com/SDWebImage/SDWebImageLottieCoder.git", from: "0.2.0")

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:

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

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

Loading

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

Animation and Size

  • Objective-C
// 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)}];
  • Swift
// 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.

  • Objective-C
// 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}];
  • Swift
// 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

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

GitHub

link
Stars: 14
Last commit: 12 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

0.1.0 - Initial version
12 weeks ago

Note

This version contains bug that can not render different JSON data at the same time. Use 0.2.0 instead.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API