cdn加载lottle动画(Image)

CDN上传

将Lottle资源打成zip包上传到云端(七牛)简单上传

本地工程处理

  • 工程安装依赖:
    坑 如果是OC工程使用swift版lottie,无法直接使用,需要进行swift封装,在进行@objcMembers文件修饰
  pod 'lottie-ios' ,'3.2.3'
  pod 'AFNetworking'
  pod 'SSZipArchive'
  pod 'SnapKit'
  • 封装HLAnimationImageView供OC使用(swift 可直接使用)
    AnimationView 是可以加载l沙盒lottle.json && image
    Animation 直接加载bundle的lottle.json 或 lottle.json && image, 或沙盒的lottle.json ,但无法引用沙盒图片
import UIKit
import SnapKit
import Lottie
@objcMembers
public class HLAnimationImageView : UIView {
    /// 动画
    public var animationView = AnimationView()
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    public init(frame: CGRect,filepath: String) {
        super.init(frame: frame)
        animationView = AnimationView.init(filePath: filepath)
        animationView.contentMode = .scaleToFill
        addSubview(animationView)
        animationView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /// 是否是循环播放动画
    public var loopAnimation: Bool {
        set {
            if newValue {
                animationView.loopMode = .loop
            } else {
                animationView.loopMode = .playOnce
            }
            animationView.backgroundBehavior = .pauseAndRestore
        }
        get {
            if animationView.loopMode == .loop {
                return true
            }
            return false
        }
    }
    /// 继续播放
    public func kep_play() {
        layoutIfNeeded()
        animationView.play()
    }
    /// 暂停
    public func kep_pause() {
        animationView.pause()
    }
    /// 停止
    public func kep_stop() {
        animationView.stop()
    }
}

下载图片解压缩,lottle播放

- (void)downloadLottleWithZipUrlStr:(NSString *)zipString complete:(void (^)(NSURL *, NSError *))complete {
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:zipString]];
  AFURLSessionManager *session = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLSessionDownloadTask *downTask = [session downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"%@",downloadProgress.localizedDescription);
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:response.suggestedFilename];
        NSURL *url = [NSURL fileURLWithPath:filePath];
        return url;
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        if (error == nil) {
            if (complete) {
                complete(filePath, nil);
            }
        } else {
            if (complete) {
                complete(nil, error);
            }
        }
    }];
    [downTask resume];
}
- (void)unzipUrlStr:(NSString *)zipString complete:(void (^)(NSString *, NSError *))complete {
    
    [self downloadLottleWithZipUrlStr:zipString complete:^(NSURL *fileUrl, NSError *error) {
        if (error != nil) {
            !complete?: complete(nil, error);
            return ;
        }
        NSString *zipPath = fileUrl.absoluteString;
        zipPath = [zipPath stringByReplacingOccurrencesOfString:@"file://" withString:@""];
        NSString *destinationPath = [self destinationPath];
        NSFileManager *manager = [NSFileManager defaultManager];
        if (![manager fileExistsAtPath:destinationPath]) {
            NSError *error = nil;
            [manager createDirectoryAtPath:destinationPath withIntermediateDirectories:true attributes:nil error:&error];
            if (error != nil) {
                !complete?: complete(nil, error);
                return ;
            }
        }
        BOOL flag = [SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
        if (flag) {
            NSLog(@"true");
            if (complete) {
                complete(destinationPath, nil);
                // 解压缩文件成功后自动删除 zip压缩包
                [manager removeItemAtPath:zipPath error:nil];
            }
        } else {
            NSLog(@"false");
            if (complete) {
                complete(nil, [NSError errorWithDomain:@"解压失败" code:300 userInfo:@{@"zipPath" : fileUrl}]);
            }
        }
    }];
}

- (NSString *)destinationPath {
    if (_destinationPath.length == 0) {
        NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"abcd"];
        return path;
    }
    return _destinationPath;
}

加载使用
lottle.json 的目录地址必须对应上,否则无法加载出图片

        let requestUrl = "https://xxxx.lottle.zip"
        manager?.destinationPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] + "/lotties"
        manager?.unzipUrlStr(requestUrl, complete: { (filePath, error) in
            if (error == nil) {
                let filePath2 = filePath! + "/xxxx/lottle.json"
                let hitHighDisplayView = KEPAnimationImageView.init(frame: self.view.bounds, filepath: filePath2)
                hitHighDisplayView.frame = self.view.bounds;
                self.view.addSubview(hitHighDisplayView)
                hitHighDisplayView.loopAnimation = false
                hitHighDisplayView.backgroundColor = UIColor.blue
                hitHighDisplayView.kep_play()
            }
        })
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • C语言相关面试题 1.static有什么用途? 答案:在C语言中,static主要定义全局静态变量,定义局部静态变...
    Leeson1989阅读 2,301评论 0 20
  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,547评论 8 265
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,588评论 28 53
  • 信任包括信任自己和信任他人 很多时候,很多事情,失败、遗憾、错过,源于不自信,不信任他人 觉得自己做不成,别人做不...
    吴氵晃阅读 6,222评论 4 8
  • 步骤:发微博01-导航栏内容 -> 发微博02-自定义TextView -> 发微博03-完善TextView和...
    dibadalu阅读 3,167评论 1 3