forked from svga/SVGAPlayer-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVGAExporter.m
More file actions
89 lines (79 loc) · 3.13 KB
/
SVGAExporter.m
File metadata and controls
89 lines (79 loc) · 3.13 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
//
// SVGAExporter.m
// SVGAPlayer
//
// Created by 崔明辉 on 2017/3/7.
// Copyright © 2017年 UED Center. All rights reserved.
//
#import "SVGAExporter.h"
#import "SVGAVideoEntity.h"
#import "SVGAVideoSpriteEntity.h"
#import "SVGAVideoSpriteFrameEntity.h"
#import "SVGAContentLayer.h"
#import "SVGAVectorLayer.h"
@interface SVGAExporter ()
@property (nonatomic, strong) CALayer *drawLayer;
@property (nonatomic, assign) NSInteger currentFrame;
@end
@implementation SVGAExporter
- (NSArray<UIImage *> *)toImages {
NSMutableArray *images = [NSMutableArray array];
if (self.videoItem != nil && self.videoItem.videoSize.width > 0.0 && self.videoItem.videoSize.height > 0.0) {
[self draw];
for (NSInteger i = 0; i < self.videoItem.frames; i++) {
self.currentFrame = i;
[self update];
UIGraphicsBeginImageContextWithOptions(self.drawLayer.frame.size, NO, 1.0);
[self.drawLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
if (image != nil) {
[images addObject:image];
}
UIGraphicsEndImageContext();
}
}
return [images copy];
}
- (void)saveImages:(NSString *)toPath filePrefix:(NSString *)filePrefix {
if (filePrefix == nil) {
filePrefix = @"";
}
[[NSFileManager defaultManager] createDirectoryAtPath:toPath withIntermediateDirectories:YES attributes:nil error:NULL];
if (self.videoItem != nil && self.videoItem.videoSize.width > 0.0 && self.videoItem.videoSize.height > 0.0) {
[self draw];
for (NSInteger i = 0; i < self.videoItem.frames; i++) {
self.currentFrame = i;
[self update];
UIGraphicsBeginImageContextWithOptions(self.drawLayer.frame.size, NO, 1.0);
[self.drawLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
if (image != nil) {
NSData *imageData = UIImagePNGRepresentation(image);
if (imageData != nil) {
[imageData writeToFile:[NSString stringWithFormat:@"%@/%@%ld.png", toPath, filePrefix, (long)i] atomically:YES];
}
}
UIGraphicsEndImageContext();
}
}
}
- (void)draw {
self.drawLayer = [[CALayer alloc] init];
self.drawLayer.frame = CGRectMake(0, 0, self.videoItem.videoSize.width, self.videoItem.videoSize.height);
self.drawLayer.masksToBounds = true;
[self.videoItem.sprites enumerateObjectsUsingBlock:^(SVGAVideoSpriteEntity * _Nonnull sprite, NSUInteger idx, BOOL * _Nonnull stop) {
UIImage *bitmap = self.videoItem.images[sprite.imageKey];;
SVGAContentLayer *contentLayer = [sprite requestLayerWithBitmap:bitmap];
[self.drawLayer addSublayer:contentLayer];
}];
self.currentFrame = 0;
[self update];
}
- (void)update {
for (SVGAContentLayer *layer in self.drawLayer.sublayers) {
if ([layer isKindOfClass:[SVGAContentLayer class]]) {
[layer stepToFrame:self.currentFrame];
}
}
}
@end