SVGAExporter.m 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // SVGAExporter.m
  3. // SVGAPlayer
  4. //
  5. // Created by 崔明辉 on 2017/3/7.
  6. // Copyright © 2017年 UED Center. All rights reserved.
  7. //
  8. #import "SVGAExporter.h"
  9. #import "SVGAVideoEntity.h"
  10. #import "SVGAVideoSpriteEntity.h"
  11. #import "SVGAVideoSpriteFrameEntity.h"
  12. #import "SVGAContentLayer.h"
  13. #import "SVGAVectorLayer.h"
  14. @interface SVGAExporter ()
  15. @property (nonatomic, strong) CALayer *drawLayer;
  16. @property (nonatomic, assign) NSInteger currentFrame;
  17. @end
  18. @implementation SVGAExporter
  19. - (NSArray<UIImage *> *)toImages {
  20. NSMutableArray *images = [NSMutableArray array];
  21. if (self.videoItem != nil && self.videoItem.videoSize.width > 0.0 && self.videoItem.videoSize.height > 0.0) {
  22. [self draw];
  23. for (NSInteger i = 0; i < self.videoItem.frames; i++) {
  24. self.currentFrame = i;
  25. [self update];
  26. UIGraphicsBeginImageContextWithOptions(self.drawLayer.frame.size, NO, 1.0);
  27. [self.drawLayer renderInContext:UIGraphicsGetCurrentContext()];
  28. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  29. if (image != nil) {
  30. [images addObject:image];
  31. }
  32. UIGraphicsEndImageContext();
  33. }
  34. }
  35. return [images copy];
  36. }
  37. - (void)saveImages:(NSString *)toPath filePrefix:(NSString *)filePrefix {
  38. if (filePrefix == nil) {
  39. filePrefix = @"";
  40. }
  41. [[NSFileManager defaultManager] createDirectoryAtPath:toPath withIntermediateDirectories:YES attributes:nil error:NULL];
  42. if (self.videoItem != nil && self.videoItem.videoSize.width > 0.0 && self.videoItem.videoSize.height > 0.0) {
  43. [self draw];
  44. for (NSInteger i = 0; i < self.videoItem.frames; i++) {
  45. self.currentFrame = i;
  46. [self update];
  47. UIGraphicsBeginImageContextWithOptions(self.drawLayer.frame.size, NO, 1.0);
  48. [self.drawLayer renderInContext:UIGraphicsGetCurrentContext()];
  49. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  50. if (image != nil) {
  51. NSData *imageData = UIImagePNGRepresentation(image);
  52. if (imageData != nil) {
  53. [imageData writeToFile:[NSString stringWithFormat:@"%@/%@%ld.png", toPath, filePrefix, (long)i] atomically:YES];
  54. }
  55. }
  56. UIGraphicsEndImageContext();
  57. }
  58. }
  59. }
  60. - (void)draw {
  61. self.drawLayer = [[CALayer alloc] init];
  62. self.drawLayer.frame = CGRectMake(0, 0, self.videoItem.videoSize.width, self.videoItem.videoSize.height);
  63. self.drawLayer.masksToBounds = true;
  64. [self.videoItem.sprites enumerateObjectsUsingBlock:^(SVGAVideoSpriteEntity * _Nonnull sprite, NSUInteger idx, BOOL * _Nonnull stop) {
  65. UIImage *bitmap = self.videoItem.images[sprite.imageKey];;
  66. SVGAContentLayer *contentLayer = [sprite requestLayerWithBitmap:bitmap];
  67. [self.drawLayer addSublayer:contentLayer];
  68. }];
  69. self.currentFrame = 0;
  70. [self update];
  71. }
  72. - (void)update {
  73. for (SVGAContentLayer *layer in self.drawLayer.sublayers) {
  74. if ([layer isKindOfClass:[SVGAContentLayer class]]) {
  75. [layer stepToFrame:self.currentFrame];
  76. }
  77. }
  78. }
  79. @end