ClipViewController.m
7.66 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
//
// ClipViewController.m
// Camera
//
// Created by wzh on 2017/6/6.
// Copyright © 2017年 wzh. All rights reserved.
//
#import "ClipViewController.h"
#import "TKImageView.h"
#import "ClipConfirmView.h"
#define SelfWidth [UIScreen mainScreen].bounds.size.width
#define SelfHeight [UIScreen mainScreen].bounds.size.height
static const CGFloat kToolBarHeight = 120.0f;
@interface ClipViewController ()
@property (nonatomic, assign) BOOL isClip;
@property (nonatomic, strong) TKImageView *tkImageView;
// 预览页面
@property (nonatomic, strong) ClipConfirmView *clipConfirmView;
// 底部工具栏
@property (nonatomic, strong) UIView *editToolBarView;
@end
@implementation ClipViewController{
// 确认按钮
UIButton *sureBtn;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor blackColor]];
[self.mainLayout setBackgroundColor:[UIColor blackColor]];
[self createdTkImageView];
[self createdTool];
[self createdClipConfirmView];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// [_editToolBarView mas_remakeConstraints:^(MASConstraintMaker *make) {
// make.bottom.left.right.mas_equalTo(self.mainLayout);
// make.height.mas_equalTo(@(kToolBarHeight));
// }];
// [_tkImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
// make.left.right.mas_equalTo(self.mainLayout);
// make.bottom.mas_equalTo(self->_editToolBarView.mas_top);
// make.height.mas_equalTo(@(SelfHeight - kToolBarHeight));
// }];
// [_clipConfirmView mas_remakeConstraints:^(MASConstraintMaker *make) {
// make.edges.mas_equalTo(self->_tkImageView);
// }];
}
- (void)createdTkImageView
{
_tkImageView = [[TKImageView alloc] initWithFrame:CGRectMake(0, 0, SelfWidth, SelfHeight - kToolBarHeight)];
// _tkImageView = [[TKImageView alloc] initWithFrame:CGRectMake(0, 0, SelfWidth, SelfHeight)];
[self.mainLayout addSubview:_tkImageView];
//需要进行裁剪的图片对象
_tkImageView.toCropImage = _image;
//是否显示中间线
_tkImageView.showMidLines = YES;
//是否需要支持缩放裁剪
_tkImageView.needScaleCrop = NO;
//是否显示九宫格交叉线
_tkImageView.showCrossLines = YES;
_tkImageView.cornerBorderInImage = NO;
_tkImageView.cropAreaCornerWidth = 44;
_tkImageView.cropAreaCornerHeight = 44;
_tkImageView.minSpace = 30;
_tkImageView.cropAreaCornerLineColor = [UIColor whiteColor];
_tkImageView.cropAreaBorderLineColor = [UIColor whiteColor];
_tkImageView.cropAreaCornerLineWidth = 6;
_tkImageView.cropAreaBorderLineWidth = 1;
_tkImageView.cropAreaMidLineWidth = 20;
_tkImageView.cropAreaMidLineHeight = 6;
_tkImageView.cropAreaMidLineColor = [UIColor whiteColor];
_tkImageView.cropAreaCrossLineColor = [UIColor whiteColor];
_tkImageView.cropAreaCrossLineWidth = 0.5;
_tkImageView.initialScaleFactor = .8f;
_tkImageView.cropAspectRatio = self.cropAspectRatio;
_tkImageView.maskColor = [UIColor clearColor];
self.isClip = NO;
}
- (void)createdTool
{
_editToolBarView = [[UIView alloc] initWithFrame:CGRectMake(0, SelfHeight - kToolBarHeight, SelfWidth, kToolBarHeight)];
_editToolBarView.backgroundColor = [UIColor blackColor];
_editToolBarView.alpha = 0.8;
[self.mainLayout addSubview:_editToolBarView];
UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
cancleBtn.frame = CGRectMake(((SelfWidth / 3.0) - 50)/2.0, (kToolBarHeight - 50)/2.0, 50, 50);
[cancleBtn setImage:[UIImage imageNamed:@"canclePhoto"] forState:UIControlStateNormal];
[cancleBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[_editToolBarView addSubview:cancleBtn];
UIButton *clipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
clipBtn.frame = CGRectMake(((SelfWidth) - 50)/2.0, (kToolBarHeight - 50)/2.0, 50, 50);
[clipBtn setImage:[UIImage imageNamed:@"clipPhoto"] forState:UIControlStateNormal];
[clipBtn setImage:[UIImage imageNamed:@"backPhoto"] forState:UIControlStateSelected];
[clipBtn addTarget:self action:@selector(clip:) forControlEvents:UIControlEventTouchUpInside];
[_editToolBarView addSubview:clipBtn];
sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
sureBtn.frame = CGRectMake(((SelfWidth/3.0) - 50)/2.0 + (SelfWidth * 2.0/3.0), (kToolBarHeight - 50)/2.0, 50, 50);
[sureBtn setImage:[UIImage imageNamed:@"surePhoto"] forState:UIControlStateNormal];
[sureBtn addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
[_editToolBarView addSubview:sureBtn];
if(_cropAspectRatio > 0) {
sureBtn.hidden = YES;
}
}
- (void)createdClipConfirmView{
_clipConfirmView = [[ClipConfirmView alloc] initWithFrame:CGRectMake(0, 0, SelfWidth, SelfHeight - kToolBarHeight)];
[self.mainLayout addSubview:_clipConfirmView];
_clipConfirmView.hidden = YES;
}
- (void)back{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)clip:(UIButton *)btn{
btn.selected = !btn.selected;
self.isClip = btn.selected;
if (btn.selected == YES) {
[_clipConfirmView setImage:[_tkImageView currentCroppedImage]];
_clipConfirmView.hidden = NO;
sureBtn.hidden = NO;
//_tkImageView.toCropImage = [_tkImageView currentCroppedImage];
}else{
_tkImageView.toCropImage = _image;
_clipConfirmView.hidden = YES;
if(_cropAspectRatio > 0) {
sureBtn.hidden = YES;
}
}
}
- (void)sure{
//裁剪
if (self.isClip == YES) {
//UIImage *image = [_tkImageView currentCroppedImage];
UIImage *image = [_clipConfirmView getClipImage];
if (self.isTakePhoto) {
//将图片存储到相册
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
}
if (self.delegate && [self.delegate respondsToSelector:@selector(clipPhoto:)]) {
[self.delegate clipPhoto:image];
}
}else{
if (self.delegate && [self.delegate respondsToSelector:@selector(clipPhoto:)]) {
[self.delegate clipPhoto:self.image];
}
if (self.isTakePhoto) {
//将图片存储到相册
UIImageWriteToSavedPhotosAlbum(self.image, self, nil, nil);
}
}
[self dismissViewControllerAnimated:YES completion:nil];
[self.picker dismissViewControllerAnimated:YES completion:nil];
[self.controller dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
/**
屏幕方向 >=6.0
*/
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate {
return NO;
}
/**
状态条样式 >=7.0
*/
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
/**
是否隐藏状态条 >=7.0
*/
- (BOOL)prefersStatusBarHidden {
return YES;
}
/**
内存预警
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
@end