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