[教程] 详解iOS如何让Lottie使用网络资源做动画的实现

[复制链接]
发表于 2023-2-3 08:35:22 | 显示全部楼层 |阅读模式

背景

手上有需求需要使用CDN资源来让Lottie做动画,但由于动画需要加载图片,而Lottie提供的初始化接口只能加载json配置,Github上的issues也没人回答,因此特地写下本文记录一下方案。

为了实现这个功能还把Lottie看了一遍也是醉了。。。

方案

首先需要明确的一个点是如果你的Lottie资源带图片,那么直接使用LOTAnimationView的initWithContentsOfURL:方法是无法自动加载图片资源的。因为加载图片需要为LOTComposition设置baseURL,但通过url初始化animatonView时,由于json配置需要异步加载,所以该view的sceneModel为空,你无法直接设置,而view内部又没有加载完成的回调,因此只能通过监听sceneModel设置或者生成一个sceneModel传入这两种方式来实现Lottie图片资源加载。

以下介绍实现方式。

1. 实现LOTAnimationDelegate代理

首先需要实现LOTAnimationView的图片请求代理方法。Lottie内部不会自行请求图片,而是通过代理方法的形式将图片请求抛到外部实现。

- (void)animationView:(LOTAnimationView *)animationView fetchResourceWithURL:(NSURL *)url completionHandler:(LOTResourceCompletionHandler)completionHandler {
    [CDNService requestLottieImageWithURL:url completion:^(UIImage * _Nullable image, NSError * _Nullable error) {
        if (completionHandler) {
            completionHandler(image, error);
        }
    }];

}

2. 生成LOTComposition

其次,由于外部业务无法直接感知LOTAnimationView内部生成的LOTComposition的时机,因此可以选择自己生成它,并设置baseURL。

+ (void)requestLottieModelWithURL:(NSURL *)url completion:(void(^)(LOTComposition * _Nullable sceneModel,  NSError * _Nullable error))completion {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
        NSData *animationData = [NSData dataWithContentsOfURL:url];
        if (!animationData) {
            return;
        }
        NSError *error;
        NSDictionary *animationJSON = [NSJSONSerialization JSONObjectWithData:animationData options:0 error:&error];
        if (error || !animationJSON) {
            if (completion) {
                completion(nil, error);
            }
            return;
        }
        LOTComposition *model = [[LOTComposition alloc] initWithJSON:animationJSON withAssetBundle:[NSBundle mainBundle]];
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            [[LOTAnimationCache sharedCache] addAnimation:model forKey:url.absoluteString];
            //注意,这里的baseURL是你的请求path,需要根据你的业务情况自行设置
            model.baseURL = @"https://os.xxx.cn/lottie/animation/";
            model.cacheKey = url.absoluteString;
            if (completion) {
                completion(model, nil);
            }
        });
    });
}

需要注意的是LOTComposition的baseURL设置,不仅需要查看Lottie的json配置文件,还需要关注服务端存储Lottie文件的路径。

假设你有一个叫animation的Lottie资源,那么请先打开配置json观察assets.u的值。这里假设assets.u为"images/",则你需要在服务端存储的文件结构如下:

- animation
    - data.json
    - images
        - img_0.png
        - img_1.png

此时,如果json的请求url是os.xxx.cn/lottie/anim… ,那么需要给LOTComposition的baseURL设置为os.xxx.cn/lottie/anim… 。

3. 初始化LOTAnimationView

最后只需要请求资源并传给LOTAnimationView即可。

- (LOTAnimationView *)animationView {
    if (!_animationView) {
        //注意,如果想先初始化view再请求资源,不要使用new或者init来初始化
        _animationView = [[LOTAnimationView alloc] initWithFrame:CGRectZero];
        _animationView.animationDelegate = self;
        NSURL *url = [NSURL URLWithString:@"https://os.xxx.cn/lottie/animation/data.json"];
        //请求json配置,生成LOTComposition后传给view
        @weakify(self);
        [CCDNService requestLottieModelWithURL:url completion:^(LOTComposition * _Nullable sceneModel, NSError * _Nullable error) {
            @strongify(self);
            self.animationView.sceneModel = sceneModel;
        }];
    }
    return _animationView;
}

以上就是iOS如何让Lottie使用网络资源做动画的详细内容,更多关于iOS Lottie网络资源做动画的资料请关注琼殿技术社区其它相关文章!

您可能感兴趣的文章:
  • iOS自定义雷达扫描扩散动画
  • iOS实现贝塞尔曲线动画
  • iOS实现逐帧动画做loading视图
  • Android实现仿iOS菊花加载圈动画效果
  • 详解 iOS 系统中的视图动画
回复

使用道具 举报

发表于 2026-10-2 03:03:08 | 显示全部楼层
感谢分享,带图片资源的 Lottie 只加载 JSON 时图片不显示这个坑确实挺常见的,楼主从代理到生成 LOTComposition 再初始化视图的思路很完整。

补充一个我遇到过的小问题:如果 CDN 上的图片是相对路径,设置 baseURL 时一定要确认结尾有没有斜杠,不然拼出来的图片地址容易少一段直接 404。另外线上使用建议把 JSON 和图片都做下缓存,否则每次进页面都重新请求会很慢。

问一下楼主,图片资源比较多或者动画比较复杂的时候,你在真机上有没有遇到内存或掉帧问题?我之前有个带大量序列帧的动画,滑动时明显卡顿,后来只能让设计压缩资源。

期待楼主后续更新。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

圆梦公社,专注于为全球华人提供纯粹技术交流的地方,请勿发布任何政治及违法的言论。如有相关侵权、举报、投诉及建议等,请发 E-mail:dzh188@hotmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部