improve: DONOT createPath until display it.

improve: add named method to parser.
This commit is contained in:
PonyCui
2017-08-21 14:29:50 +08:00
parent 40d3d96a1d
commit 0b41def110
3 changed files with 55 additions and 5 deletions

View File

@@ -8,9 +8,20 @@
#import "SVGABezierPath.h"
@interface SVGABezierPath ()
@property (nonatomic, assign) BOOL displaying;
@property (nonatomic, copy) NSString *backValues;
@end
@implementation SVGABezierPath
- (void)setValues:(nonnull NSString *)values {
if (!self.displaying) {
self.backValues = values;
return;
}
static NSArray *validMethods;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
@@ -51,6 +62,10 @@
}
- (nonnull CAShapeLayer *)createLayer {
if (!self.displaying) {
self.displaying = YES;
[self setValues:self.backValues];
}
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = self.CGPath;
layer.fillColor = [UIColor blackColor].CGColor;

View File

@@ -21,4 +21,9 @@
completionBlock:(void ( ^ _Nullable)(SVGAVideoEntity * _Nonnull videoItem))completionBlock
failureBlock:(void ( ^ _Nullable)(NSError * _Nonnull error))failureBlock;
- (void)parseWithNamed:(nonnull NSString *)named
inBundle:(nullable NSBundle *)inBundle
completionBlock:(void ( ^ _Nullable)(SVGAVideoEntity * _Nonnull videoItem))completionBlock
failureBlock:(void ( ^ _Nullable)(NSError * _Nonnull error))failureBlock;
@end

View File

@@ -61,6 +61,23 @@ static NSOperationQueue *parseQueue;
}] resume];
}
- (void)parseWithNamed:(NSString *)named
inBundle:(NSBundle *)inBundle
completionBlock:(void (^)(SVGAVideoEntity * _Nonnull))completionBlock
failureBlock:(void (^)(NSError * _Nonnull))failureBlock {
NSString *filePath = [(inBundle ?: [NSBundle mainBundle]) pathForResource:named ofType:@"svga"];
if (filePath != nil) {
NSString *cacheKey = [self cacheKey:[NSURL fileURLWithPath:filePath]];
[self parseWithData:[NSData dataWithContentsOfFile:filePath]
cacheKey:cacheKey
completionBlock:completionBlock
failureBlock:failureBlock];
}
else {
failureBlock([NSError errorWithDomain:@"SVGAParser" code:404 userInfo:@{NSLocalizedDescriptionKey: @"File not exist."}]);
}
}
- (void)parseWithCacheKey:(nonnull NSString *)cacheKey
completionBlock:(void ( ^ _Nullable)(SVGAVideoEntity * _Nonnull videoItem))completionBlock
failureBlock:(void ( ^ _Nullable)(NSError * _Nonnull error))failureBlock {
@@ -104,12 +121,25 @@ static NSOperationQueue *parseQueue;
cacheKey:(nonnull NSString *)cacheKey
completionBlock:(void ( ^ _Nullable)(SVGAVideoEntity * _Nonnull videoItem))completionBlock
failureBlock:(void ( ^ _Nullable)(NSError * _Nonnull error))failureBlock {
SVGAVideoEntity *cacheItem = [SVGAVideoEntity readCache:cacheKey];
if (cacheItem != nil) {
if (completionBlock) {
completionBlock(cacheItem);
}
return;
}
[parseQueue addOperationWithBlock:^{
SVGAVideoEntity *cacheItem = [SVGAVideoEntity readCache:cacheKey];
if (cacheItem != nil) {
if (completionBlock) {
completionBlock(cacheItem);
}
if ([[NSFileManager defaultManager] fileExistsAtPath:[self cacheDirectory:cacheKey]]) {
[self parseWithCacheKey:cacheKey completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
if (completionBlock) {
completionBlock(videoItem);
}
} failureBlock:^(NSError * _Nonnull error) {
[self clearCache:cacheKey];
if (failureBlock) {
failureBlock(error);
}
}];
return;
}
NSString *tmpPath = [NSTemporaryDirectory() stringByAppendingFormat:@"%u.svga", arc4random()];