SVGAVideoSpriteEntity.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. NSString *matteKey = JSONObject[@"matteKey"];
  21. NSArray<NSDictionary *> *JSONFrames = JSONObject[@"frames"];
  22. if ([imageKey isKindOfClass:[NSString class]] && [JSONFrames isKindOfClass:[NSArray class]]) {
  23. NSMutableArray<SVGAVideoSpriteFrameEntity *> *frames = [[NSMutableArray alloc] init];
  24. [JSONFrames enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  25. if ([obj isKindOfClass:[NSDictionary class]]) {
  26. [frames addObject:[[SVGAVideoSpriteFrameEntity alloc] initWithJSONObject:obj]];
  27. }
  28. }];
  29. _imageKey = imageKey;
  30. _frames = frames;
  31. _matteKey = matteKey;
  32. }
  33. }
  34. }
  35. return self;
  36. }
  37. - (instancetype)initWithProtoObject:(SVGAProtoSpriteEntity *)protoObject {
  38. self = [super init];
  39. if (self) {
  40. if ([protoObject isKindOfClass:[SVGAProtoSpriteEntity class]]) {
  41. NSString *imageKey = protoObject.imageKey;
  42. NSString *matteKey = protoObject.matteKey;
  43. NSArray<NSDictionary *> *protoFrames = [protoObject.framesArray copy];
  44. if ([imageKey isKindOfClass:[NSString class]] && [protoFrames isKindOfClass:[NSArray class]]) {
  45. NSMutableArray<SVGAVideoSpriteFrameEntity *> *frames = [[NSMutableArray alloc] init];
  46. [protoFrames enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  47. if ([obj isKindOfClass:[SVGAProtoFrameEntity class]]) {
  48. [frames addObject:[[SVGAVideoSpriteFrameEntity alloc] initWithProtoObject:obj]];
  49. }
  50. }];
  51. _imageKey = imageKey;
  52. _frames = frames;
  53. _matteKey = matteKey;
  54. }
  55. }
  56. }
  57. return self;
  58. }
  59. - (SVGAContentLayer *)requestLayerWithBitmap:(UIImage *)bitmap {
  60. SVGAContentLayer *layer = [[SVGAContentLayer alloc] initWithFrames:self.frames];
  61. if (bitmap != nil) {
  62. layer.bitmapLayer = [[SVGABitmapLayer alloc] initWithFrames:self.frames];
  63. layer.bitmapLayer.contents = (__bridge id _Nullable)([bitmap CGImage]);
  64. }
  65. layer.vectorLayer = [[SVGAVectorLayer alloc] initWithFrames:self.frames];
  66. return layer;
  67. }
  68. @end