美文网首页
cdn加载lottle动画(Image)

cdn加载lottle动画(Image)

作者: 一抹相思泪成雨 | 来源:发表于2021-06-10 22:59 被阅读0次

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()
            }
        })

相关文章

  • cdn加载lottle动画(Image)

    CDN上传 将Lottle资源打成zip包上传到云端(七牛)简单上传 本地工程处理 工程安装依赖:坑 如果是OC工...

  • iOS动画Animation

    1.UIKit层面的动画 帧动画,UIImage实现的动画 1.image动画,用一组image加载为闪图 2.I...

  • 2018-07-10 自定义网络加载动画

    58同城网络加载动画效果动画链接: //upload-images.jianshu.io/upload_image...

  • 3. Image组件

    Image Image.network(): 加载远程图片Image.asset(): 加载本地图片 显示网络图片...

  • Vue学习记录

    Here[http://cdn.hellooooo.top/image/blog/2020/09/vue/vue....

  • 2018-12-27

    flutter加载本地图片加载不出来 flutter Image 加载图片可以分为4中情况,分别为: Image....

  • Widget-Image

    Image Image.asset:加载资源图片 Image.network:网络资源图片 Image.fi...

  • 阿里云cdn用法

    最近公司图片加载缓慢,所以用到cdn,其实cdn原理就是智能选择最优服务器缓存加载图片,就和数据库添加索引查找比没...

  • crypto加密:后端PHP加密,前端JS解密

    前端工作: 使用cdn加载: 但有时build上传到服务器,浏览器加载不了cdn地址,可以另存为上传到自家服务器上...

  • 学习小组DAY6笔记——小游

    安装和加载R包 1.镜像设置image.png 2.安装image.png 3.加载image.png dplyr...

网友评论

      本文标题:cdn加载lottle动画(Image)

      本文链接:https://www.haomeiwen.com/subject/cwjqeltx.html