SDWebImageLottieCoder

CI Status Version License Platform SwiftPM compatible Carthage compatible

简介

这是 Lottie 动画格式 的一个解码器插件。

区别

我们已经构建了一个 Lottie 插件,名为 SDWebImageLottiePlugin

这两个组件的主要区别在于我们如何播放动画以及我们使用什么依赖。为了减少不必要依赖的代码大小,我们将它们分成了 2 个不同的仓库。

SDWebImageLottiePlugin

这个 Lottie 框架依赖于 lottie-ios,它由 Airbnb 维护。

这个插件只能使用他们自己的 LOTAnimationView 来播放动画。

SDWebImageLottieCoder

这个 Lottie 框架依赖于 rlottie,它由三星维护。

这个插件可以在 SDAnimatedImageViewUIImageView/NSImageView 上播放动画。

示例

要运行示例项目,请克隆 repo,然后首先从 Example 目录运行 pod install

要求

安装

CocoaPods

SDWebImageLottieCoder 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile

pod 'SDWebImageLottieCoder'

Carthage

SDWebImageLottieCoder 可通过 Carthage 获得。

github "SDWebImage/SDWebImageLottieCoder"

Swift Package Manager (Xcode 11+)

SDWebImageLottieCoder 可通过 Swift Package Manager 获得。

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

SDWebImageLottieCoder 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile

pod 'SDWebImageLottieCoder'

用法

添加解码器

在使用 SDWebImage 加载 Lottie json 之前,您需要将 Lottie 解码器注册到您的解码器管理器。 建议在您的 App 启动后完成此步骤(例如 AppDelegate 方法)。

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

加载

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

动画和大小

// 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])

解码

您也可以将 Lottie 图像解码为动画 UIImage/NSImage。 如果 Lottie 图像有 引用的外部图像资源,您也可以指定它。

// 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])

截图

这些 Lottie 动画贴纸来自 lottiefiles-telegram

作者

DreamPiggy, lizhuoli1126@126.com

许可证

SDWebImageLottieCoder 在 MIT 许可下可用。 有关更多信息,请参见 LICENSE 文件。

鸣谢