mirror of
https://github.com/svga/SVGAPlayer-iOS.git
synced 2025-10-21 07:29:14 +08:00
Add Local Cache.
This commit is contained in:
@@ -148,6 +148,7 @@
|
||||
TargetAttributes = {
|
||||
90A676DC1D13A6DF008A69F3 = {
|
||||
CreatedOnToolsVersion = 7.3;
|
||||
DevelopmentTeam = 8M2FQ87SLP;
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -307,7 +308,9 @@
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
|
||||
CODE_SIGN_IDENTITY = "iPhone Developer";
|
||||
INFOPLIST_FILE = SVGAPlayer/Info.plist;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.opensource.SVGAPlayer;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
@@ -319,7 +322,9 @@
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
|
||||
CODE_SIGN_IDENTITY = "iPhone Developer";
|
||||
INFOPLIST_FILE = SVGAPlayer/Info.plist;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.opensource.SVGAPlayer;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
|
@@ -21,13 +21,15 @@
|
||||
[super viewDidLoad];
|
||||
[self.view addSubview:self.aPlayer];
|
||||
self.aPlayer.frame = CGRectMake(0, 0, 320, 100);
|
||||
self.aPlayer.loops = 10;
|
||||
self.aPlayer.clearsAfterStop = YES;
|
||||
SVGAParser *parser = [[SVGAParser alloc] init];
|
||||
[parser parseWithURL:[NSURL URLWithString:@"http://uedfe.yypm.com/assets/test.svga"] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
|
||||
if (videoItem != nil) {
|
||||
self.aPlayer.videoItem = videoItem;
|
||||
[self.aPlayer startAnimation];
|
||||
}
|
||||
}];
|
||||
} failureBlock:nil];
|
||||
}
|
||||
|
||||
- (void)viewWillLayoutSubviews {
|
||||
|
@@ -7,11 +7,17 @@
|
||||
//
|
||||
|
||||
#import "SVGADownloader.h"
|
||||
#import "NSData+GZIP.h"
|
||||
#import <AVKit/AVKit.h>
|
||||
|
||||
@implementation SVGADownloader
|
||||
|
||||
- (void)loadDataWithURL:(NSURL *)URL completionBlock:(void (^)(NSData *))completionBlock failureBlock:(void (^)(NSError *))failureBlock {
|
||||
NSData *cacheData = [self readCacheWithURL:URL];
|
||||
if (cacheData != nil) {
|
||||
completionBlock(cacheData);
|
||||
return;
|
||||
}
|
||||
[[[NSURLSession sharedSession] dataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
||||
if (error != nil || data == nil) {
|
||||
if (failureBlock) {
|
||||
@@ -19,6 +25,7 @@
|
||||
}
|
||||
}
|
||||
else {
|
||||
[self saveCacheWithURL:URL data:data];
|
||||
if (completionBlock) {
|
||||
completionBlock(data);
|
||||
}
|
||||
@@ -26,4 +33,20 @@
|
||||
}] resume];
|
||||
}
|
||||
|
||||
- (nullable NSData *)readCacheWithURL:(NSURL *)URL {
|
||||
NSString *cacheFilePath = [[URL absoluteString] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
|
||||
return [NSData dataWithContentsOfFile:[NSTemporaryDirectory() stringByAppendingString:cacheFilePath] options:kNilOptions error:nil];
|
||||
}
|
||||
|
||||
- (void)saveCacheWithURL:(nonnull NSURL *)URL data:(nonnull NSData *)data {
|
||||
if (data != nil) {
|
||||
NSData *unzipData = [data svga_gunzippedData];
|
||||
if (unzipData != nil) {
|
||||
data = unzipData;
|
||||
}
|
||||
NSString *cacheFilePath = [[URL absoluteString] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
|
||||
[data writeToFile:[NSTemporaryDirectory() stringByAppendingString:cacheFilePath] atomically:YES];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
@@ -12,7 +12,9 @@
|
||||
|
||||
@interface SVGAParser : NSObject
|
||||
|
||||
- (void)parseWithURL:(nonnull NSURL *)URL completionBlock:(void ( ^ _Nonnull )(SVGAVideoEntity * _Nullable videoItem))completionBlock;
|
||||
- (void)parseWithURL:(nonnull NSURL *)URL
|
||||
completionBlock:(void ( ^ _Nonnull )(SVGAVideoEntity * _Nullable videoItem))completionBlock
|
||||
failureBlock:(void ( ^ _Nullable)(NSError * _Nullable error))failureBlock;
|
||||
|
||||
- (nullable SVGAVideoEntity *)parseWithData:(nonnull NSData *)data;
|
||||
|
||||
|
@@ -19,13 +19,17 @@
|
||||
|
||||
@implementation SVGAParser
|
||||
|
||||
- (void)parseWithURL:(nonnull NSURL *)URL completionBlock:(void ( ^ _Nonnull )(SVGAVideoEntity * _Nullable videoItem))completionBlock {
|
||||
- (void)parseWithURL:(nonnull NSURL *)URL
|
||||
completionBlock:(void ( ^ _Nonnull )(SVGAVideoEntity * _Nullable videoItem))completionBlock
|
||||
failureBlock:(void ( ^ _Nullable)(NSError * _Nullable error))failureBlock {
|
||||
[self.downloader loadDataWithURL:URL completionBlock:^(NSData *data) {
|
||||
if (completionBlock) {
|
||||
completionBlock([self parseWithData:data]);
|
||||
}
|
||||
} failureBlock:^(NSError *error) {
|
||||
// failure
|
||||
if (failureBlock) {
|
||||
failureBlock(error);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
|
@@ -13,6 +13,9 @@
|
||||
@interface SVGAPlayer : UIView
|
||||
|
||||
@property (nonatomic, strong) SVGAVideoEntity *videoItem;
|
||||
@property (nonatomic, assign) int loops;
|
||||
@property (nonatomic, assign) BOOL clearsAfterStop;
|
||||
|
||||
|
||||
- (void)startAnimation;
|
||||
- (void)stopAnimation;
|
||||
|
@@ -9,7 +9,9 @@
|
||||
#import "SVGAPlayer.h"
|
||||
#import "SVGAVideoEntity.h"
|
||||
|
||||
@interface SVGAPlayer ()
|
||||
@interface SVGAPlayer () {
|
||||
int _loopCount;
|
||||
}
|
||||
|
||||
@property (nonatomic, strong) CALayer *drawLayer;
|
||||
@property (nonatomic, strong) CADisplayLink *displayLink;
|
||||
@@ -20,18 +22,29 @@
|
||||
@implementation SVGAPlayer
|
||||
|
||||
- (void)startAnimation {
|
||||
[self.displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
[self stopAnimation:NO];
|
||||
_loopCount = 0;
|
||||
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)];
|
||||
self.displayLink.frameInterval = 60 / self.videoItem.FPS;
|
||||
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
}
|
||||
|
||||
- (void)stopAnimation {
|
||||
[self.displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
[self stopAnimation:self.clearsAfterStop];
|
||||
}
|
||||
|
||||
- (void)stopAnimation:(BOOL)clear {
|
||||
if (![self.displayLink isPaused]) {
|
||||
[self.displayLink setPaused:YES];
|
||||
[self.displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
}
|
||||
if (clear) {
|
||||
[self clear];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)clear {
|
||||
[self.drawLayer.sublayers performSelector:@selector(removeFromSuperlayer)];
|
||||
[self.drawLayer removeFromSuperlayer];
|
||||
}
|
||||
|
||||
- (void)draw {
|
||||
@@ -93,6 +106,10 @@
|
||||
self.currentFrame++;
|
||||
if (self.currentFrame >= [self.videoItem.sprites firstObject].frames.count) {
|
||||
self.currentFrame = 0;
|
||||
_loopCount++;
|
||||
if (self.loops > 0 && _loopCount >= self.loops) {
|
||||
[self stopAnimation];
|
||||
}
|
||||
}
|
||||
[self update];
|
||||
}
|
||||
|
Reference in New Issue
Block a user