VPImageCropperViewController.m
12.6 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
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
//
// VPImageCropperViewController.m
// VPolor
//
// Created by Vinson.D.Warm on 12/30/13.
// Copyright (c) 2013 Huang Vinson. All rights reserved.
//
#import "VPImageCropperViewController.h"
#define SCALE_FRAME_Y 100.0f
#define BOUNDCE_DURATION 0.3f
@interface VPImageCropperViewController ()
@property (nonatomic, retain) UIImage *originalImage;
@property (nonatomic, retain) UIImage *editedImage;
@property (nonatomic, retain) UIImageView *showImgView;
@property (nonatomic, retain) UIView *overlayView;
@property (nonatomic, retain) UIView *ratioView;
@property (nonatomic, assign) CGRect oldFrame;
@property (nonatomic, assign) CGRect largeFrame;
@property (nonatomic, assign) CGFloat limitRatio;
@property (nonatomic, assign) CGRect latestFrame;
@end
@implementation VPImageCropperViewController
- (id)initWithImage:(UIImage *)originalImage cropFrame:(CGRect)cropFrame limitScaleRatio:(NSInteger)limitRatio {
self = [super init];
if (self) {
self.cropFrame = cropFrame;
self.limitRatio = limitRatio;
self.originalImage = originalImage;
}
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
[self initView];
[self initControlBtn];
}
- (void)dealloc {
self.originalImage = nil;
self.showImgView = nil;
self.editedImage = nil;
self.overlayView = nil;
self.ratioView = nil;
}
#pragma mark - UI
- (void)initView {
self.showImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.showImgView setMultipleTouchEnabled:YES];
[self.showImgView setUserInteractionEnabled:YES];
[self.showImgView setImage:self.originalImage];
[self.showImgView setUserInteractionEnabled:YES];
[self.showImgView setMultipleTouchEnabled:YES];
// scale to fit the screen
CGFloat oriWidth = self.cropFrame.size.width;
CGFloat oriHeight = self.originalImage.size.height * (oriWidth / self.originalImage.size.width);
CGFloat oriX = self.cropFrame.origin.x + (self.cropFrame.size.width - oriWidth) / 2;
CGFloat oriY = self.cropFrame.origin.y + (self.cropFrame.size.height - oriHeight) / 2;
self.oldFrame = CGRectMake(oriX, oriY, oriWidth, oriHeight);
self.latestFrame = self.oldFrame;
self.showImgView.frame = self.oldFrame;
self.largeFrame = CGRectMake(0, 0, self.limitRatio * self.oldFrame.size.width, self.limitRatio * self.oldFrame.size.height);
[self addGestureRecognizers];
[self.view addSubview:self.showImgView];
self.overlayView = [[UIView alloc] initWithFrame:self.view.bounds];
self.overlayView.alpha = .5f;
self.overlayView.backgroundColor = [UIColor blackColor];
self.overlayView.userInteractionEnabled = NO;
self.overlayView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.overlayView];
//黄色框
self.ratioView = [[UIView alloc] initWithFrame:self.cropFrame];
self.ratioView.alpha = .5f;
self.ratioView.backgroundColor = [UIColor blackColor];
self.ratioView.layer.borderColor = [UIColor yellowColor].CGColor;
self.ratioView.layer.borderWidth = 1.0f;
self.ratioView.autoresizingMask = UIViewAutoresizingNone;
[self.view addSubview:self.ratioView];
[self overlayClipping];
}
- (void)initControlBtn {
UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50.0f, 100, 50)];
cancelBtn.backgroundColor = [UIColor clearColor];
cancelBtn.titleLabel.textColor = [UIColor whiteColor];
[cancelBtn setTitle:CustomStr(@"Cancel") forState:UIControlStateNormal];
[cancelBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:18.0f]];
[cancelBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
[cancelBtn.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
[cancelBtn.titleLabel setNumberOfLines:0];
[cancelBtn setTitleEdgeInsets:UIEdgeInsetsMake(5.0f, 5.0f, 5.0f, 5.0f)];
[cancelBtn addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cancelBtn];
UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 100.0f, self.view.frame.size.height - 50.0f, 100, 50)];
confirmBtn.backgroundColor = [UIColor clearColor];
confirmBtn.titleLabel.textColor = [UIColor whiteColor];
[confirmBtn setTitle:CustomStr(@"Sure") forState:UIControlStateNormal];
[confirmBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:18.0f]];
[confirmBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
confirmBtn.titleLabel.textColor = [UIColor whiteColor];
[confirmBtn.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
[confirmBtn.titleLabel setNumberOfLines:0];
[confirmBtn setTitleEdgeInsets:UIEdgeInsetsMake(5.0f, 5.0f, 5.0f, 5.0f)];
[confirmBtn addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:confirmBtn];
}
#pragma mark - Action
- (void)cancel:(id)sender {
if (self.delegate && [self.delegate conformsToProtocol:@protocol(VPImageCropperDelegate)]) {
[self.delegate imageCropperDidCancel:self];
}
}
- (void)confirm:(id)sender {
if (self.delegate && [self.delegate conformsToProtocol:@protocol(VPImageCropperDelegate)]) {
[self.delegate imageCropper:self didFinished:[self getSubImage]];
}
}
- (void)overlayClipping{
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGMutablePathRef path = CGPathCreateMutable();
// Left side of the ratio view
CGPathAddRect(path, nil, CGRectMake(0, 0,
self.ratioView.frame.origin.x,
self.overlayView.frame.size.height));
// Right side of the ratio view
CGPathAddRect(path, nil, CGRectMake(
self.ratioView.frame.origin.x + self.ratioView.frame.size.width,
0,
self.overlayView.frame.size.width - self.ratioView.frame.origin.x - self.ratioView.frame.size.width,
self.overlayView.frame.size.height));
// Top side of the ratio view
CGPathAddRect(path, nil, CGRectMake(0, 0,
self.overlayView.frame.size.width,
self.ratioView.frame.origin.y));
// Bottom side of the ratio view
CGPathAddRect(path, nil, CGRectMake(0,
self.ratioView.frame.origin.y + self.ratioView.frame.size.height,
self.overlayView.frame.size.width,
self.overlayView.frame.size.height - self.ratioView.frame.origin.y + self.ratioView.frame.size.height));
maskLayer.path = path;
self.overlayView.layer.mask = maskLayer;
CGPathRelease(path);
}
// register all gestures
- (void) addGestureRecognizers{
// add pinch gesture
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
[self.view addGestureRecognizer:pinchGestureRecognizer];
// add pan gesture
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
[self.view addGestureRecognizer:panGestureRecognizer];
}
// pinch gesture handler
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer{
UIView *view = self.showImgView;
if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
pinchGestureRecognizer.scale = 1;
}
else if (pinchGestureRecognizer.state == UIGestureRecognizerStateEnded) {
CGRect newFrame = self.showImgView.frame;
newFrame = [self handleScaleOverflow:newFrame];
newFrame = [self handleBorderOverflow:newFrame];
[UIView animateWithDuration:BOUNDCE_DURATION animations:^{
self.showImgView.frame = newFrame;
self.latestFrame = newFrame;
}];
}
}
// pan gesture handler
- (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer{
UIView *view = self.showImgView;
if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
// calculate accelerator
CGFloat absCenterX = self.cropFrame.origin.x + self.cropFrame.size.width / 2;
CGFloat absCenterY = self.cropFrame.origin.y + self.cropFrame.size.height / 2;
CGFloat scaleRatio = self.showImgView.frame.size.width / self.cropFrame.size.width;
CGFloat acceleratorX = 1 - ABS(absCenterX - view.center.x) / (scaleRatio * absCenterX);
CGFloat acceleratorY = 1 - ABS(absCenterY - view.center.y) / (scaleRatio * absCenterY);
CGPoint translation = [panGestureRecognizer translationInView:view.superview];
[view setCenter:(CGPoint){view.center.x + translation.x * acceleratorX, view.center.y + translation.y * acceleratorY}];
[panGestureRecognizer setTranslation:CGPointZero inView:view.superview];
}
else if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
// bounce to original frame
CGRect newFrame = self.showImgView.frame;
newFrame = [self handleBorderOverflow:newFrame];
[UIView animateWithDuration:BOUNDCE_DURATION animations:^{
self.showImgView.frame = newFrame;
self.latestFrame = newFrame;
}];
}
}
- (CGRect)handleScaleOverflow:(CGRect)newFrame {
// bounce to original frame
CGPoint oriCenter = CGPointMake(newFrame.origin.x + newFrame.size.width/2, newFrame.origin.y + newFrame.size.height/2);
if (newFrame.size.width < self.oldFrame.size.width) {
newFrame = self.oldFrame;
}
if (newFrame.size.width > self.largeFrame.size.width) {
newFrame = self.largeFrame;
}
newFrame.origin.x = oriCenter.x - newFrame.size.width/2;
newFrame.origin.y = oriCenter.y - newFrame.size.height/2;
return newFrame;
}
- (CGRect)handleBorderOverflow:(CGRect)newFrame {
// horizontally
if (newFrame.origin.x > self.cropFrame.origin.x) newFrame.origin.x = self.cropFrame.origin.x;
if (CGRectGetMaxX(newFrame) < self.cropFrame.size.width) newFrame.origin.x = self.cropFrame.size.width - newFrame.size.width;
// vertically
if (newFrame.origin.y > self.cropFrame.origin.y) newFrame.origin.y = self.cropFrame.origin.y;
if (CGRectGetMaxY(newFrame) < self.cropFrame.origin.y + self.cropFrame.size.height) {
newFrame.origin.y = self.cropFrame.origin.y + self.cropFrame.size.height - newFrame.size.height;
}
// adapt horizontally rectangle
if (self.showImgView.frame.size.width > self.showImgView.frame.size.height && newFrame.size.height <= self.cropFrame.size.height) {
newFrame.origin.y = self.cropFrame.origin.y + (self.cropFrame.size.height - newFrame.size.height) / 2;
}
return newFrame;
}
- (UIImage *)getSubImage{
CGRect squareFrame = self.cropFrame;
CGFloat scaleRatio = self.latestFrame.size.width / self.originalImage.size.width;
CGFloat x = (squareFrame.origin.x - self.latestFrame.origin.x) / scaleRatio;
CGFloat y = (squareFrame.origin.y - self.latestFrame.origin.y) / scaleRatio;
CGFloat w = squareFrame.size.width / scaleRatio;
CGFloat h = squareFrame.size.width / scaleRatio;
if (self.latestFrame.size.width < self.cropFrame.size.width) {
CGFloat newW = self.originalImage.size.width;
CGFloat newH = newW * (self.cropFrame.size.height / self.cropFrame.size.width);
x = 0; y = y + (h - newH) / 2;
w = newH; h = newH;
}
if (self.latestFrame.size.height < self.cropFrame.size.height) {
CGFloat newH = self.originalImage.size.height;
CGFloat newW = newH * (self.cropFrame.size.width / self.cropFrame.size.height);
x = x + (w - newW) / 2; y = 0;
w = newH; h = newH;
}
CGRect myImageRect = CGRectMake(x, y, w, h);
CGImageRef imageRef = self.originalImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
CGSize size;
size.width = myImageRect.size.width;
size.height = myImageRect.size.height;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return smallImage;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return NO;
}
@end