SVGAVideoEntity.m 6.9 KB

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