SVGAVideoSpriteEntity.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // SVGAVideoSpriteEntity.m
  3. // SVGAPlayer
  4. //
  5. // Created by 崔明辉 on 2017/2/20.
  6. // Copyright © 2017年 UED Center. All rights reserved.
  7. //
  8. #import "SVGAVideoSpriteEntity.h"
  9. #import "SVGAVideoSpriteFrameEntity.h"
  10. #import "SVGABitmapLayer.h"
  11. #import "SVGAContentLayer.h"
  12. #import "SVGAVectorLayer.h"
  13. #import "Svga.pbobjc.h"
  14. @implementation SVGAVideoSpriteEntity
  15. - (instancetype)initWithJSONObject:(NSDictionary *)JSONObject {
  16. self = [super init];
  17. if (self) {
  18. if ([JSONObject isKindOfClass:[NSDictionary class]]) {
  19. NSString *imageKey = JSONObject[@"imageKey"];
  20. NSArray<NSDictionary *> *JSONFrames = JSONObject[@"frames"];
  21. if ([imageKey isKindOfClass:[NSString class]] && [JSONFrames isKindOfClass:[NSArray class]]) {
  22. NSMutableArray<SVGAVideoSpriteFrameEntity *> *frames = [[NSMutableArray alloc] init];
  23. [JSONFrames enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  24. if ([obj isKindOfClass:[NSDictionary class]]) {
  25. [frames addObject:[[SVGAVideoSpriteFrameEntity alloc] initWithJSONObject:obj]];
  26. }
  27. }];
  28. _imageKey = imageKey;
  29. _frames = frames;
  30. }
  31. }
  32. }
  33. return self;
  34. }
  35. - (instancetype)initWithProtoObject:(SVGAProtoSpriteEntity *)protoObject {
  36. self = [super init];
  37. if (self) {
  38. if ([protoObject isKindOfClass:[SVGAProtoSpriteEntity class]]) {
  39. NSString *imageKey = protoObject.imageKey;
  40. NSArray<NSDictionary *> *protoFrames = [protoObject.framesArray copy];
  41. if ([imageKey isKindOfClass:[NSString class]] && [protoFrames isKindOfClass:[NSArray class]]) {
  42. NSMutableArray<SVGAVideoSpriteFrameEntity *> *frames = [[NSMutableArray alloc] init];
  43. [protoFrames enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  44. if ([obj isKindOfClass:[SVGAProtoFrameEntity class]]) {
  45. [frames addObject:[[SVGAVideoSpriteFrameEntity alloc] initWithProtoObject:obj]];
  46. }
  47. }];
  48. _imageKey = imageKey;
  49. _frames = frames;
  50. }
  51. }
  52. }
  53. return self;
  54. }
  55. - (SVGAContentLayer *)requestLayerWithBitmap:(UIImage *)bitmap {
  56. SVGAContentLayer *layer = [[SVGAContentLayer alloc] initWithFrames:self.frames];
  57. if (bitmap != nil) {
  58. layer.bitmapLayer = [[SVGABitmapLayer alloc] initWithFrames:self.frames];
  59. layer.bitmapLayer.contents = (__bridge id _Nullable)([bitmap CGImage]);
  60. }
  61. layer.vectorLayer = [[SVGAVectorLayer alloc] initWithFrames:self.frames];
  62. return layer;
  63. }
  64. @end