这是 Lottie 动画格式 的一个解码器插件。
我们已经构建了一个 Lottie 插件,名为 SDWebImageLottiePlugin。
这两个组件的主要区别在于我们如何播放动画
以及我们使用什么依赖
。为了减少不必要依赖的代码大小,我们将它们分成了 2 个不同的仓库。
这个 Lottie 框架依赖于 lottie-ios,它由 Airbnb 维护。
这个插件只能使用他们自己的 LOTAnimationView
来播放动画。
优点:它使用像 Core Animation Layer 这样的矢量渲染技术,这意味着您可以动态地改变视图大小而不会丢失细节或重新生成图像。
缺点:矢量渲染比位图渲染慢得多。对于小而大量的 Lottie 图像,例如表情符号、小图标,这不适用。
这个 Lottie 框架依赖于 rlottie,它由三星维护。
这个插件可以在 SDAnimatedImageView 和 UIImageView/NSImageView
上播放动画。
优点:它使用位图渲染,每个动画帧都被渲染成栅格化的位图,而不是矢量图像。您还可以将所有帧预加载到内存中,以获得最佳性能和 60FPS。 这也很容易集成到 UIKit/AppKit 原生框架中。
缺点:位图渲染不支持动态大小更改。一旦您想要更大的图像,您需要重新解码源 Lottie JSON,这既耗时又耗费 RAM。
要运行示例项目,请克隆 repo,然后首先从 Example 目录运行 pod install
。
SDWebImageLottieCoder 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile
pod 'SDWebImageLottieCoder'
SDWebImageLottieCoder 可通过 Carthage 获得。
github "SDWebImage/SDWebImageLottieCoder"
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 文件。