/
ICGVideoTrimmerView.m
executable file
·411 lines (326 loc) · 15.5 KB
/
ICGVideoTrimmerView.m
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//
// ICGVideoTrimmerView.m
// ICGVideoTrimmer
//
// Created by Huong Do on 1/18/15.
// Copyright (c) 2015 ichigo. All rights reserved.
//
#import "ICGVideoTrimmerView.h"
#import "ICGThumbView.h"
#import "ICGRulerView.h"
@interface ICGVideoTrimmerView() <UIScrollViewDelegate>
@property (strong, nonatomic) UIView *contentView;
@property (strong, nonatomic) UIView *frameView;
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) AVAssetImageGenerator *imageGenerator;
@property (strong, nonatomic) UIView *leftOverlayView;
@property (strong, nonatomic) UIView *rightOverlayView;
@property (strong, nonatomic) ICGThumbView *leftThumbView;
@property (strong, nonatomic) ICGThumbView *rightThumbView;
@property (strong, nonatomic) UIView *trackerView;
@property (strong, nonatomic) UIView *topBorder;
@property (strong, nonatomic) UIView *bottomBorder;
@property (nonatomic) CGFloat startTime;
@property (nonatomic) CGFloat endTime;
@property (nonatomic) CGFloat widthPerSecond;
@property (nonatomic) CGPoint leftStartPoint;
@property (nonatomic) CGPoint rightStartPoint;
@property (nonatomic) CGFloat overlayWidth;
@end
@implementation ICGVideoTrimmerView
#pragma mark - Initiation
- (instancetype)initWithFrame:(CGRect)frame
{
NSAssert(NO, nil);
@throw nil;
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
return [super initWithCoder:aDecoder];
}
- (instancetype)initWithAsset:(AVAsset *)asset
{
return [self initWithFrame:CGRectZero asset:asset];
}
- (instancetype)initWithFrame:(CGRect)frame asset:(AVAsset *)asset
{
self = [super initWithFrame:frame];
if (self) {
_asset = asset;
[self resetSubviews];
}
return self;
}
#pragma mark - Private methods
- (UIColor *)themeColor
{
return _themeColor ?: [UIColor lightGrayColor];
}
- (CGFloat)maxLength
{
return _maxLength ?: 15;
}
- (CGFloat)minLength
{
return _minLength ?: 3;
}
- (UIColor *)trackerColor
{
return _trackerColor ?: [UIColor whiteColor];
}
- (CGFloat)borderWidth
{
return _borderWidth ?: 1;
}
- (CGFloat)thumbWidth
{
return _thumbWidth ?: 10;
}
- (void)resetSubviews
{
self.clipsToBounds = YES;
[self setBackgroundColor:[UIColor blackColor]];
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
[self addSubview:self.scrollView];
[self.scrollView setDelegate:self];
[self.scrollView setShowsHorizontalScrollIndicator:NO];
self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame))];
[self.scrollView setContentSize:self.contentView.frame.size];
[self.scrollView addSubview:self.contentView];
CGFloat ratio = self.showsRulerView ? 0.7 : 1.0;
self.frameView = [[UIView alloc] initWithFrame:CGRectMake(self.thumbWidth, 0, CGRectGetWidth(self.contentView.frame)-2*self.thumbWidth, CGRectGetHeight(self.contentView.frame)*ratio)];
[self.frameView.layer setMasksToBounds:YES];
[self.contentView addSubview:self.frameView];
[self addFrames];
if (self.showsRulerView) {
CGRect rulerFrame = CGRectMake(0, CGRectGetHeight(self.contentView.frame)*0.7, CGRectGetWidth(self.contentView.frame)+self.thumbWidth, CGRectGetHeight(self.contentView.frame)*0.3);
ICGRulerView *rulerView = [[ICGRulerView alloc] initWithFrame:rulerFrame widthPerSecond:self.widthPerSecond themeColor:self.themeColor];
[self.contentView addSubview:rulerView];
}
// add borders
self.topBorder = [[UIView alloc] init];
[self.topBorder setBackgroundColor:self.themeColor];
[self addSubview:self.topBorder];
self.bottomBorder = [[UIView alloc] init];
[self.bottomBorder setBackgroundColor:self.themeColor];
[self addSubview:self.bottomBorder];
// width for left and right overlay views
self.overlayWidth = CGRectGetWidth(self.frame) - (self.minLength * self.widthPerSecond);
// add left overlay view
self.leftOverlayView = [[UIView alloc] initWithFrame:CGRectMake(self.thumbWidth - self.overlayWidth, 0, self.overlayWidth, CGRectGetHeight(self.frameView.frame))];
CGRect leftThumbFrame = CGRectMake(self.overlayWidth-self.thumbWidth, 0, self.thumbWidth, CGRectGetHeight(self.frameView.frame));
if (self.leftThumbImage) {
self.leftThumbView = [[ICGThumbView alloc] initWithFrame:leftThumbFrame thumbImage:self.leftThumbImage];
} else {
self.leftThumbView = [[ICGThumbView alloc] initWithFrame:leftThumbFrame color:self.themeColor right:NO];
}
self.trackerView = [[UIView alloc] initWithFrame:CGRectMake(self.thumbWidth, -5, 3, CGRectGetHeight(self.frameView.frame) + 10)];
self.trackerView.backgroundColor = self.trackerColor;
self.trackerView.layer.masksToBounds = true;
self.trackerView.layer.cornerRadius = 2;
[self addSubview:self.trackerView];
[self.leftThumbView.layer setMasksToBounds:YES];
[self.leftOverlayView addSubview:self.leftThumbView];
[self.leftOverlayView setUserInteractionEnabled:YES];
UIPanGestureRecognizer *leftPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveLeftOverlayView:)];
[self.leftOverlayView addGestureRecognizer:leftPanGestureRecognizer];
[self.leftOverlayView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.8]];
[self addSubview:self.leftOverlayView];
// add right overlay view
CGFloat rightViewFrameX = CGRectGetWidth(self.frameView.frame) < CGRectGetWidth(self.frame) ? CGRectGetMaxX(self.frameView.frame) : CGRectGetWidth(self.frame) - self.thumbWidth;
self.rightOverlayView = [[UIView alloc] initWithFrame:CGRectMake(rightViewFrameX, 0, self.overlayWidth, CGRectGetHeight(self.frameView.frame))];
if (self.rightThumbImage) {
self.rightThumbView = [[ICGThumbView alloc] initWithFrame:CGRectMake(0, 0, self.thumbWidth, CGRectGetHeight(self.frameView.frame)) thumbImage:self.rightThumbImage];
} else {
self.rightThumbView = [[ICGThumbView alloc] initWithFrame:CGRectMake(0, 0, self.thumbWidth, CGRectGetHeight(self.frameView.frame)) color:self.themeColor right:YES];
}
[self.rightThumbView.layer setMasksToBounds:YES];
[self.rightOverlayView addSubview:self.rightThumbView];
[self.rightOverlayView setUserInteractionEnabled:YES];
UIPanGestureRecognizer *rightPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveRightOverlayView:)];
[self.rightOverlayView addGestureRecognizer:rightPanGestureRecognizer];
[self.rightOverlayView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.8]];
[self addSubview:self.rightOverlayView];
[self updateBorderFrames];
[self notifyDelegate];
}
- (void)updateBorderFrames
{
CGFloat height = self.borderWidth;
[self.topBorder setFrame:CGRectMake(CGRectGetMaxX(self.leftOverlayView.frame), 0, CGRectGetMinX(self.rightOverlayView.frame)-CGRectGetMaxX(self.leftOverlayView.frame), height)];
[self.bottomBorder setFrame:CGRectMake(CGRectGetMaxX(self.leftOverlayView.frame), CGRectGetHeight(self.frameView.frame)-height, CGRectGetMinX(self.rightOverlayView.frame)-CGRectGetMaxX(self.leftOverlayView.frame), height)];
}
- (void)moveLeftOverlayView:(UIPanGestureRecognizer *)gesture
{
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
self.leftStartPoint = [gesture locationInView:self];
break;
case UIGestureRecognizerStateChanged:
{
CGPoint point = [gesture locationInView:self];
int deltaX = point.x - self.leftStartPoint.x;
CGPoint center = self.leftOverlayView.center;
CGFloat newLeftViewMidX = center.x += deltaX;;
CGFloat maxWidth = CGRectGetMinX(self.rightOverlayView.frame) - (self.minLength * self.widthPerSecond);
CGFloat newLeftViewMinX = newLeftViewMidX - self.overlayWidth/2;
if (newLeftViewMinX < self.thumbWidth - self.overlayWidth) {
newLeftViewMidX = self.thumbWidth - self.overlayWidth + self.overlayWidth/2;
} else if (newLeftViewMinX + self.overlayWidth > maxWidth) {
newLeftViewMidX = maxWidth - self.overlayWidth / 2;
}
self.leftOverlayView.center = CGPointMake(newLeftViewMidX, self.leftOverlayView.center.y);
self.leftStartPoint = point;
[self updateBorderFrames];
[self notifyDelegate];
break;
}
default:
break;
}
}
- (void)moveRightOverlayView:(UIPanGestureRecognizer *)gesture
{
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
self.rightStartPoint = [gesture locationInView:self];
break;
case UIGestureRecognizerStateChanged:
{
CGPoint point = [gesture locationInView:self];
int deltaX = point.x - self.rightStartPoint.x;
CGPoint center = self.rightOverlayView.center;
CGFloat newRightViewMidX = center.x += deltaX;
CGFloat minX = CGRectGetMaxX(self.leftOverlayView.frame) + self.minLength * self.widthPerSecond;
CGFloat maxX = CMTimeGetSeconds([self.asset duration]) <= self.maxLength + 0.5 ? CGRectGetMaxX(self.frameView.frame) : CGRectGetWidth(self.frame) - self.thumbWidth;
if (newRightViewMidX - self.overlayWidth/2 < minX) {
newRightViewMidX = minX + self.overlayWidth/2;
} else if (newRightViewMidX - self.overlayWidth/2 > maxX) {
newRightViewMidX = maxX + self.overlayWidth/2;
}
self.rightOverlayView.center = CGPointMake(newRightViewMidX, self.rightOverlayView.center.y);
self.rightStartPoint = point;
[self updateBorderFrames];
[self notifyDelegate];
break;
}
default:
break;
}
}
- (void)seekToTime:(CGFloat) time
{
CGFloat posToMove = time * self.widthPerSecond + self.thumbWidth - self.scrollView.contentOffset.x;
CGRect trackerFrame = self.trackerView.frame;
trackerFrame.origin.x = posToMove;
self.trackerView.frame = trackerFrame;
}
- (void)hideTracker:(BOOL)flag
{
self.trackerView.hidden = flag;
}
- (void)notifyDelegate
{
CGFloat start = CGRectGetMaxX(self.leftOverlayView.frame) / self.widthPerSecond + (self.scrollView.contentOffset.x -self.thumbWidth) / self.widthPerSecond;
if (!self.trackerView.hidden && start != self.startTime) {
[self seekToTime:start];
}
self.startTime = start;
self.endTime = CGRectGetMinX(self.rightOverlayView.frame) / self.widthPerSecond + (self.scrollView.contentOffset.x - self.thumbWidth) / self.widthPerSecond;
[self.delegate trimmerView:self didChangeLeftPosition:self.startTime rightPosition:self.endTime];
}
- (void)addFrames
{
self.imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:self.asset];
self.imageGenerator.appliesPreferredTrackTransform = YES;
if ([self isRetina]){
self.imageGenerator.maximumSize = CGSizeMake(CGRectGetWidth(self.frameView.frame)*2, CGRectGetHeight(self.frameView.frame)*2);
} else {
self.imageGenerator.maximumSize = CGSizeMake(CGRectGetWidth(self.frameView.frame), CGRectGetHeight(self.frameView.frame));
}
CGFloat picWidth = 0;
// First image
NSError *error;
CMTime actualTime;
CGImageRef halfWayImage = [self.imageGenerator copyCGImageAtTime:kCMTimeZero actualTime:&actualTime error:&error];
UIImage *videoScreen;
if ([self isRetina]){
videoScreen = [[UIImage alloc] initWithCGImage:halfWayImage scale:2.0 orientation:UIImageOrientationUp];
} else {
videoScreen = [[UIImage alloc] initWithCGImage:halfWayImage];
}
if (halfWayImage != NULL) {
UIImageView *tmp = [[UIImageView alloc] initWithImage:videoScreen];
CGRect rect = tmp.frame;
rect.size.width = videoScreen.size.width;
tmp.frame = rect;
[self.frameView addSubview:tmp];
picWidth = tmp.frame.size.width / 3;
CGImageRelease(halfWayImage);
}
Float64 duration = CMTimeGetSeconds([self.asset duration]);
CGFloat screenWidth = CGRectGetWidth(self.frame) - 2*self.thumbWidth; // quick fix to make up for the width of thumb views
NSInteger actualFramesNeeded;
CGFloat frameViewFrameWidth = (duration / self.maxLength) * screenWidth;
[self.frameView setFrame:CGRectMake(self.thumbWidth, 0, frameViewFrameWidth, CGRectGetHeight(self.frameView.frame))];
CGFloat contentViewFrameWidth = CMTimeGetSeconds([self.asset duration]) <= self.maxLength + 0.5 ? screenWidth + 30 : frameViewFrameWidth;
[self.contentView setFrame:CGRectMake(0, 0, contentViewFrameWidth, CGRectGetHeight(self.contentView.frame))];
[self.scrollView setContentSize:self.contentView.frame.size];
NSInteger minFramesNeeded = screenWidth / picWidth + 1;
actualFramesNeeded = (duration / self.maxLength) * minFramesNeeded + 1;
Float64 durationPerFrame = duration / (actualFramesNeeded*1.0);
self.widthPerSecond = frameViewFrameWidth / duration;
int preferredWidth = 0;
NSMutableArray *times = [[NSMutableArray alloc] init];
for (int i=1; i<actualFramesNeeded; i++){
CMTime time = CMTimeMakeWithSeconds(i*durationPerFrame, 600);
[times addObject:[NSValue valueWithCMTime:time]];
UIImageView *tmp = [[UIImageView alloc] initWithImage:videoScreen];
tmp.tag = i;
CGRect currentFrame = tmp.frame;
currentFrame.origin.x = i*picWidth;
currentFrame.size.width = picWidth;
preferredWidth += currentFrame.size.width;
if( i == actualFramesNeeded-1){
currentFrame.size.width-=6;
}
tmp.frame = currentFrame;
dispatch_async(dispatch_get_main_queue(), ^{
[self.frameView addSubview:tmp];
});
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i=1; i<=[times count]; i++) {
CMTime time = [((NSValue *)[times objectAtIndex:i-1]) CMTimeValue];
CGImageRef halfWayImage = [self.imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *videoScreen;
if ([self isRetina]){
videoScreen = [[UIImage alloc] initWithCGImage:halfWayImage scale:2.0 orientation:UIImageOrientationUp];
} else {
videoScreen = [[UIImage alloc] initWithCGImage:halfWayImage];
}
CGImageRelease(halfWayImage);
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *imageView = (UIImageView *)[self.frameView viewWithTag:i];
[imageView setImage:videoScreen];
});
}
});
}
- (BOOL)isRetina
{
return ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
([UIScreen mainScreen].scale > 1.0));
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (CMTimeGetSeconds([self.asset duration]) <= self.maxLength + 0.5) {
[UIView animateWithDuration:0.3 animations:^{
[scrollView setContentOffset:CGPointZero];
}];
}
[self notifyDelegate];
}
@end