-
Notifications
You must be signed in to change notification settings - Fork 3
/
PECropView.m
533 lines (432 loc) · 18.3 KB
/
PECropView.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//
// PECropView.m
// PhotoCropEditor
//
// Created by kishikawa katsumi on 2013/05/19.
// Copyright (c) 2013 kishikawa katsumi. All rights reserved.
//
#import "PECropView.h"
#import "PECropRectView.h"
#import "UIImage+PECrop.h"
static const CGFloat MarginTop = 44.0f;
//static const CGFloat MarginBottom = MarginTop;
static const CGFloat MarginLeft = 20.0f;
//static const CGFloat MarginRight = MarginLeft;
@interface PECropView () <UIScrollViewDelegate, UIGestureRecognizerDelegate, PECropRectViewDelegate>
@property (nonatomic) UIScrollView *scrollView;
@property (nonatomic) UIView *zoomingView;
@property (nonatomic) UIImageView *imageView;
@property (nonatomic) PECropRectView *cropRectView;
@property (nonatomic) UIView *topOverlayView;
@property (nonatomic) UIView *leftOverlayView;
@property (nonatomic) UIView *rightOverlayView;
@property (nonatomic) UIView *bottomOverlayView;
@property (nonatomic) CGRect insetRect;
@property (nonatomic) CGRect editingRect;
@property (nonatomic, getter = isResizing) BOOL resizing;
@property (nonatomic) UIInterfaceOrientation interfaceOrientation;
@end
@implementation PECropView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self commonInit];
}
return self;
}
- (void)commonInit
{
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundColor = [UIColor clearColor];
self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
self.scrollView.delegate = self;
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
self.scrollView.backgroundColor = [UIColor clearColor];
self.scrollView.maximumZoomScale = 1.54f;
self.scrollView.minimumZoomScale=1.54f;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.bounces = NO;
self.scrollView.bouncesZoom = NO;
self.scrollView.clipsToBounds = NO;
[self addSubview:self.scrollView];
//UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];
//rotationGestureRecognizer.delegate = self;
//_rotationGestureRecognizer = rotationGestureRecognizer;
//[self.scrollView addGestureRecognizer:rotationGestureRecognizer];
self.cropRectView = [[PECropRectView alloc] init];
self.cropRectView.delegate = self;
[self addSubview:self.cropRectView];
//todo
self.topOverlayView = [[UIView alloc] init];
self.topOverlayView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
[self addSubview:self.topOverlayView];
self.leftOverlayView = [[UIView alloc] init];
self.leftOverlayView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
[self addSubview:self.leftOverlayView];
self.rightOverlayView = [[UIView alloc] init];
self.rightOverlayView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
[self addSubview:self.rightOverlayView];
self.bottomOverlayView = [[UIView alloc] init];
self.bottomOverlayView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
[self addSubview:self.bottomOverlayView];
}
- (void)setShowsGridMajor:(BOOL)showsGridMajor
{
_showsGridMajor = showsGridMajor;
self.cropRectView.showsGridMajor=_showsGridMajor;
[self setNeedsDisplay];
}
#pragma mark -
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (!self.userInteractionEnabled) {
return nil;
}
UIView *hitView = [self.cropRectView hitTest:[self convertPoint:point toView:self.cropRectView] withEvent:event];
if (hitView) {
return hitView;
}
CGPoint locationInImageView = [self convertPoint:point toView:self.zoomingView];
CGPoint zoomedPoint = CGPointMake(locationInImageView.x * self.scrollView.zoomScale, locationInImageView.y * self.scrollView.zoomScale);
if (CGRectContainsPoint(self.zoomingView.frame, zoomedPoint)) {
return self.scrollView;
}
return [super hitTest:point withEvent:event];
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (!self.image) {
return;
}
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
self.editingRect = CGRectInset(self.bounds, MarginLeft, MarginTop);
} else {
self.editingRect = CGRectInset(self.bounds, MarginLeft, MarginLeft);
}
if (!self.imageView) {
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
self.insetRect = CGRectInset(self.bounds, MarginLeft, MarginTop);
} else {
self.insetRect = CGRectInset(self.bounds, MarginLeft, MarginLeft);
}
[self setupImageView];
}
if (!self.isResizing) {
[self layoutCropRectViewWithCropRect:self.scrollView.frame];
if (self.interfaceOrientation != interfaceOrientation) {
[self zoomToCropRect:self.scrollView.frame];
}
}
self.interfaceOrientation = interfaceOrientation;
}
- (void)layoutCropRectViewWithCropRect:(CGRect)cropRect
{
self.cropRectView.frame = cropRect;
[self layoutOverlayViewsWithCropRect:cropRect];
}
- (void)layoutOverlayViewsWithCropRect:(CGRect)cropRect
{
self.topOverlayView.frame = CGRectMake(0.0f,
0.0f,
CGRectGetWidth(self.bounds),
CGRectGetMinY(cropRect));
self.leftOverlayView.frame = CGRectMake(0.0f,
CGRectGetMinY(cropRect),
CGRectGetMinX(cropRect),
CGRectGetHeight(cropRect));
self.rightOverlayView.frame = CGRectMake(CGRectGetMaxX(cropRect),
CGRectGetMinY(cropRect),
CGRectGetWidth(self.bounds) - CGRectGetMaxX(cropRect),
CGRectGetHeight(cropRect));
self.bottomOverlayView.frame = CGRectMake(0.0f,
CGRectGetMaxY(cropRect),
CGRectGetWidth(self.bounds),
CGRectGetHeight(self.bounds) - CGRectGetMaxY(cropRect));
}
- (void)setupImageView
{
CGRect cropRect = AVMakeRectWithAspectRatioInsideRect(self.image.size, self.insetRect);
self.scrollView.frame = cropRect;
self.scrollView.contentSize = cropRect.size;
self.zoomingView = [[UIView alloc] initWithFrame:self.scrollView.bounds];
self.zoomingView.backgroundColor = [UIColor clearColor];
[self.scrollView addSubview:self.zoomingView];
self.imageView = [[UIImageView alloc] initWithFrame:self.zoomingView.bounds];
self.imageView.backgroundColor = [UIColor clearColor];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.image = self.image;
[self.zoomingView addSubview:self.imageView];
}
#pragma mark -
- (void)setImage:(UIImage *)image
{
_image = image;
[self.imageView removeFromSuperview];
self.imageView = nil;
[self.zoomingView removeFromSuperview];
self.zoomingView = nil;
[self setNeedsLayout];
}
- (void)setKeepingCropAspectRatio:(BOOL)keepingCropAspectRatio
{
_keepingCropAspectRatio = keepingCropAspectRatio;
self.cropRectView.keepingAspectRatio = self.keepingCropAspectRatio;
}
- (void)setCropAspectRatio:(CGFloat)aspectRatio andCenter:(BOOL)center
{
CGRect cropRect = self.scrollView.frame;
CGFloat width = CGRectGetWidth(cropRect);
CGFloat height = CGRectGetHeight(cropRect);
if (aspectRatio <= 1.0f) {
width = height * aspectRatio;
if (width > CGRectGetWidth(self.imageView.bounds)) {
width = CGRectGetWidth(cropRect);
height = width / aspectRatio;
}
} else {
height = width / aspectRatio;
if (height > CGRectGetHeight(self.imageView.bounds)) {
height = CGRectGetHeight(cropRect);
width = height * aspectRatio;
}
}
cropRect.size = CGSizeMake(width, height);
[self zoomToCropRect:cropRect andCenter:center];
}
- (void)setCropAspectRatio:(CGFloat)aspectRatio
{
[self setCropAspectRatio:aspectRatio andCenter:YES];
}
- (CGFloat)cropAspectRatio
{
CGRect cropRect = self.scrollView.frame;
CGFloat width = CGRectGetWidth(cropRect);
CGFloat height = CGRectGetHeight(cropRect);
return width / height;
}
- (void)setCropRect:(CGRect)cropRect
{
[self zoomToCropRect:cropRect];
}
- (CGRect)cropRect
{
return self.scrollView.frame;
}
- (void)setImageCropRect:(CGRect)imageCropRect
{
[self resetCropRect];
CGRect scrollViewFrame = self.scrollView.frame;
CGSize imageSize = self.image.size;
CGFloat scale = MIN(CGRectGetWidth(scrollViewFrame) / imageSize.width,
CGRectGetHeight(scrollViewFrame) / imageSize.height);
CGFloat x = CGRectGetMinX(imageCropRect) * scale + CGRectGetMinX(scrollViewFrame);
CGFloat y = CGRectGetMinY(imageCropRect) * scale + CGRectGetMinY(scrollViewFrame);
CGFloat width = CGRectGetWidth(imageCropRect) * scale;
CGFloat height = CGRectGetHeight(imageCropRect) * scale;
CGRect rect = CGRectMake(x, y, width, height);
CGRect intersection = CGRectIntersection(rect, scrollViewFrame);
if (!CGRectIsNull(intersection)) {
self.cropRect = intersection;
}
}
- (void)resetCropRect
{
[self resetCropRectAnimated:NO];
}
- (void)resetCropRectAnimated:(BOOL)animated
{
if (animated) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
[UIView setAnimationBeginsFromCurrentState:YES];
}
self.imageView.transform = CGAffineTransformIdentity;
CGSize contentSize = self.scrollView.contentSize;
CGRect initialRect = CGRectMake(0.0f, 0.0f, contentSize.width, contentSize.height);
[self.scrollView zoomToRect:initialRect animated:NO];
self.scrollView.bounds = self.imageView.bounds;
[self layoutCropRectViewWithCropRect:self.scrollView.bounds];
if (animated) {
[UIView commitAnimations];
}
}
- (UIImage *)croppedImage
{
return [self.image rotatedImageWithtransform:self.rotation croppedToRect:self.zoomedCropRect];
}
- (CGRect)zoomedCropRect
{
CGRect cropRect = [self convertRect:self.scrollView.frame toView:self.zoomingView];
CGSize size = self.image.size;
CGFloat ratio = 1.0f;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad || UIInterfaceOrientationIsPortrait(orientation)) {
ratio = CGRectGetWidth(AVMakeRectWithAspectRatioInsideRect(self.image.size, self.insetRect)) / size.width;
} else {
ratio = CGRectGetHeight(AVMakeRectWithAspectRatioInsideRect(self.image.size, self.insetRect)) / size.height;
}
CGRect zoomedCropRect = CGRectMake(cropRect.origin.x / ratio,
cropRect.origin.y / ratio,
cropRect.size.width / ratio,
cropRect.size.height / ratio);
return zoomedCropRect;
}
- (BOOL)userHasModifiedCropArea
{
CGRect zoomedCropRect = CGRectIntegral(self.zoomedCropRect);
return (!CGPointEqualToPoint(zoomedCropRect.origin, CGPointZero) ||
!CGSizeEqualToSize(zoomedCropRect.size, self.image.size) ||
!CGAffineTransformEqualToTransform(self.rotation, CGAffineTransformIdentity));
}
- (CGAffineTransform)rotation
{
return self.imageView.transform;
}
- (CGFloat)rotationAngle
{
CGAffineTransform rotation = self.imageView.transform;
return atan2f(rotation.b, rotation.a);
}
- (void)setRotationAngle:(CGFloat)rotationAngle
{
self.imageView.transform = CGAffineTransformMakeRotation(rotationAngle);
}
- (void)setRotationAngle:(CGFloat)rotationAngle snap:(BOOL)snap
{
if (snap)
{
rotationAngle = nearbyintf(rotationAngle / M_PI_2) * M_PI_2;
}
self.rotationAngle = rotationAngle;
}
- (CGRect)cappedCropRectInImageRectWithCropRectView:(PECropRectView *)cropRectView
{
CGRect cropRect = cropRectView.frame;
CGRect rect = [self convertRect:cropRect toView:self.scrollView];
if (CGRectGetMinX(rect) < CGRectGetMinX(self.zoomingView.frame)) {
cropRect.origin.x = CGRectGetMinX([self.scrollView convertRect:self.zoomingView.frame toView:self]);
CGFloat cappedWidth = CGRectGetMaxX(rect);
cropRect.size = CGSizeMake(cappedWidth,
!self.keepingCropAspectRatio ? cropRect.size.height : cropRect.size.height * (cappedWidth/cropRect.size.width));
}
if (CGRectGetMinY(rect) < CGRectGetMinY(self.zoomingView.frame)) {
cropRect.origin.y = CGRectGetMinY([self.scrollView convertRect:self.zoomingView.frame toView:self]);
CGFloat cappedHeight = CGRectGetMaxY(rect);
cropRect.size = CGSizeMake(!self.keepingCropAspectRatio ? cropRect.size.width : cropRect.size.width * (cappedHeight / cropRect.size.height),
cappedHeight);
}
if (CGRectGetMaxX(rect) > CGRectGetMaxX(self.zoomingView.frame)) {
CGFloat cappedWidth = CGRectGetMaxX([self.scrollView convertRect:self.zoomingView.frame toView:self]) - CGRectGetMinX(cropRect);
cropRect.size = CGSizeMake(cappedWidth,
!self.keepingCropAspectRatio ? cropRect.size.height : cropRect.size.height * (cappedWidth/cropRect.size.width));
}
if (CGRectGetMaxY(rect) > CGRectGetMaxY(self.zoomingView.frame)) {
CGFloat cappedHeight = CGRectGetMaxY([self.scrollView convertRect:self.zoomingView.frame toView:self]) - CGRectGetMinY(cropRect);
cropRect.size = CGSizeMake(!self.keepingCropAspectRatio ? cropRect.size.width : cropRect.size.width * (cappedHeight / cropRect.size.height),
cappedHeight);
}
return cropRect;
}
- (void)automaticZoomIfEdgeTouched:(CGRect)cropRect
{
if (CGRectGetMinX(cropRect) < CGRectGetMinX(self.editingRect) - 5.0f ||
CGRectGetMaxX(cropRect) > CGRectGetMaxX(self.editingRect) + 5.0f ||
CGRectGetMinY(cropRect) < CGRectGetMinY(self.editingRect) - 5.0f ||
CGRectGetMaxY(cropRect) > CGRectGetMaxY(self.editingRect) + 5.0f) {
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[self zoomToCropRect:self.cropRectView.frame];
} completion:NULL];
}
}
#pragma mark -
- (void)cropRectViewDidBeginEditing:(PECropRectView *)cropRectView
{
self.resizing = YES;
}
- (void)cropRectViewEditingChanged:(PECropRectView *)cropRectView
{
CGRect cropRect = [self cappedCropRectInImageRectWithCropRectView:cropRectView];
[self layoutCropRectViewWithCropRect:cropRect];
[self automaticZoomIfEdgeTouched:cropRect];
}
- (void)cropRectViewDidEndEditing:(PECropRectView *)cropRectView
{
self.resizing = NO;
[self zoomToCropRect:self.cropRectView.frame];
}
- (void)zoomToCropRect:(CGRect)toRect andCenter:(BOOL)center
{
if (CGRectEqualToRect(self.scrollView.frame, toRect)) {
return;
}
CGFloat width = CGRectGetWidth(toRect);
CGFloat height = CGRectGetHeight(toRect);
CGFloat scale = MIN(CGRectGetWidth(self.editingRect) / width, CGRectGetHeight(self.editingRect) / height);
CGFloat scaledWidth = width * scale;
CGFloat scaledHeight = height * scale;
CGRect cropRect = CGRectMake((CGRectGetWidth(self.bounds) - scaledWidth) / 2,
(CGRectGetHeight(self.bounds) - scaledHeight) / 2,
scaledWidth,
scaledHeight);
CGRect zoomRect = [self convertRect:toRect toView:self.zoomingView];
zoomRect.size.width = CGRectGetWidth(cropRect) / (self.scrollView.zoomScale * scale);
zoomRect.size.height = CGRectGetHeight(cropRect) / (self.scrollView.zoomScale * scale);
if(center) {
CGRect imageViewBounds = self.imageView.bounds;
zoomRect.origin.y = (CGRectGetHeight(imageViewBounds) / 2) - (CGRectGetHeight(zoomRect) / 2);
zoomRect.origin.x = (CGRectGetWidth(imageViewBounds) / 2) - (CGRectGetWidth(zoomRect) / 2);
}
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.scrollView.bounds = cropRect;
[self.scrollView zoomToRect:zoomRect animated:NO];
[self layoutCropRectViewWithCropRect:cropRect];
} completion:NULL];
}
- (void)zoomToCropRect:(CGRect)toRect
{
[self zoomToCropRect:toRect andCenter:NO];
}
#pragma mark -
- (void)handleRotation:(UIRotationGestureRecognizer *)gestureRecognizer
{
// CGFloat rotation = gestureRecognizer.rotation;
//
// CGAffineTransform transform = CGAffineTransformRotate(self.imageView.transform, rotation);
// self.imageView.transform = transform;
// gestureRecognizer.rotation = 0.0f;
//
// if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
// self.cropRectView.showsGridMinor = YES;
// } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded ||
// gestureRecognizer.state == UIGestureRecognizerStateCancelled ||
// gestureRecognizer.state == UIGestureRecognizerStateFailed) {
// self.cropRectView.showsGridMinor = NO;
// }
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.zoomingView;
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
CGPoint contentOffset = scrollView.contentOffset;
*targetContentOffset = contentOffset;
}
@end