forked from svga/SVGAPlayer-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVGAVideoEntity.m
More file actions
188 lines (166 loc) · 6.93 KB
/
SVGAVideoEntity.m
File metadata and controls
188 lines (166 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// SVGAVideoEntity.m
// SVGAPlayer
//
// Created by 崔明辉 on 16/6/17.
// Copyright © 2016年 UED Center. All rights reserved.
//
#import "SVGAVideoEntity.h"
#import "SVGABezierPath.h"
#import "SVGAVideoSpriteEntity.h"
#import "Svga.pbobjc.h"
@interface SVGAVideoEntity ()
@property (nonatomic, assign) CGSize videoSize;
@property (nonatomic, assign) int FPS;
@property (nonatomic, assign) int frames;
@property (nonatomic, copy) NSDictionary<NSString *, UIImage *> *images;
@property (nonatomic, copy) NSArray<SVGAVideoSpriteEntity *> *sprites;
@property (nonatomic, copy) NSString *cacheDir;
@end
@implementation SVGAVideoEntity
static NSCache *videoCache;
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
videoCache = [[NSCache alloc] init];
});
}
- (instancetype)initWithJSONObject:(NSDictionary *)JSONObject cacheDir:(NSString *)cacheDir {
self = [super init];
if (self) {
_videoSize = CGSizeMake(100, 100);
_FPS = 20;
_images = @{};
_cacheDir = cacheDir;
[self resetMovieWithJSONObject:JSONObject];
}
return self;
}
- (void)resetMovieWithJSONObject:(NSDictionary *)JSONObject {
if ([JSONObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *movieObject = JSONObject[@"movie"];
if ([movieObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *viewBox = movieObject[@"viewBox"];
if ([viewBox isKindOfClass:[NSDictionary class]]) {
NSNumber *width = viewBox[@"width"];
NSNumber *height = viewBox[@"height"];
if ([width isKindOfClass:[NSNumber class]] && [height isKindOfClass:[NSNumber class]]) {
_videoSize = CGSizeMake(width.floatValue, height.floatValue);
}
}
NSNumber *FPS = movieObject[@"fps"];
if ([FPS isKindOfClass:[NSNumber class]]) {
_FPS = [FPS intValue];
}
NSNumber *frames = movieObject[@"frames"];
if ([frames isKindOfClass:[NSNumber class]]) {
_frames = [frames intValue];
}
}
}
}
- (void)resetImagesWithJSONObject:(NSDictionary *)JSONObject {
if ([JSONObject isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary<NSString *, UIImage *> *images = [[NSMutableDictionary alloc] init];
NSDictionary<NSString *, NSString *> *JSONImages = JSONObject[@"images"];
if ([JSONImages isKindOfClass:[NSDictionary class]]) {
[JSONImages enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[NSString class]]) {
NSString *filePath = [self.cacheDir stringByAppendingFormat:@"/%@.png", obj];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
if (imageData != nil) {
UIImage *image = [[UIImage alloc] initWithData:imageData scale:2.0];
if (image != nil) {
[images setObject:image forKey:key];
}
}
}
}];
}
self.images = images;
}
}
- (void)resetSpritesWithJSONObject:(NSDictionary *)JSONObject {
if ([JSONObject isKindOfClass:[NSDictionary class]]) {
NSMutableArray<SVGAVideoSpriteEntity *> *sprites = [[NSMutableArray alloc] init];
NSArray<NSDictionary *> *JSONSprites = JSONObject[@"sprites"];
if ([JSONSprites isKindOfClass:[NSArray class]]) {
[JSONSprites enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[NSDictionary class]]) {
SVGAVideoSpriteEntity *spriteItem = [[SVGAVideoSpriteEntity alloc] initWithJSONObject:obj];
[sprites addObject:spriteItem];
}
}];
}
self.sprites = sprites;
}
}
- (instancetype)initWithProtoObject:(SVGAProtoMovieEntity *)protoObject cacheDir:(NSString *)cacheDir {
self = [super init];
if (self) {
_videoSize = CGSizeMake(100, 100);
_FPS = 20;
_images = @{};
_cacheDir = cacheDir;
[self resetMovieWithProtoObject:protoObject];
}
return self;
}
- (void)resetMovieWithProtoObject:(SVGAProtoMovieEntity *)protoObject {
if (protoObject.hasParams) {
self.videoSize = CGSizeMake((CGFloat)protoObject.params.viewBoxWidth, (CGFloat)protoObject.params.viewBoxHeight);
self.FPS = (int)protoObject.params.fps;
self.frames = (int)protoObject.params.frames;
}
}
- (void)resetImagesWithProtoObject:(SVGAProtoMovieEntity *)protoObject {
NSMutableDictionary<NSString *, UIImage *> *images = [[NSMutableDictionary alloc] init];
NSDictionary *protoImages = [protoObject.images copy];
for (NSString *key in protoImages) {
NSString *fileName = [[NSString alloc] initWithData:protoImages[key] encoding:NSUTF8StringEncoding];
if (fileName != nil) {
NSString *filePath = [self.cacheDir stringByAppendingFormat:@"/%@.png", fileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
filePath = [self.cacheDir stringByAppendingFormat:@"/%@", fileName];
}
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
if (imageData != nil) {
UIImage *image = [[UIImage alloc] initWithData:imageData scale:2.0];
if (image != nil) {
[images setObject:image forKey:key];
}
}
}
}
else {
UIImage *image = [[UIImage alloc] initWithData:protoImages[key] scale:2.0];
if (image != nil) {
[images setObject:image forKey:key];
}
}
}
self.images = images;
}
- (void)resetSpritesWithProtoObject:(SVGAProtoMovieEntity *)protoObject {
NSMutableArray<SVGAVideoSpriteEntity *> *sprites = [[NSMutableArray alloc] init];
NSArray *protoSprites = [protoObject.spritesArray copy];
[protoSprites enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[SVGAProtoSpriteEntity class]]) {
SVGAVideoSpriteEntity *spriteItem = [[SVGAVideoSpriteEntity alloc] initWithProtoObject:obj];
[sprites addObject:spriteItem];
}
}];
self.sprites = sprites;
}
+ (SVGAVideoEntity *)readCache:(NSString *)cacheKey {
return [videoCache objectForKey:cacheKey];
}
- (void)saveCache:(NSString *)cacheKey {
[videoCache setObject:self forKey:cacheKey];
}
@end
@interface SVGAVideoSpriteEntity()
@property (nonatomic, copy) NSString *imageKey;
@property (nonatomic, copy) NSArray<SVGAVideoSpriteFrameEntity *> *frames;
@end