服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - IOS 照片编辑的view封装的实例详解

IOS 照片编辑的view封装的实例详解

2021-03-26 15:57小白猪jianjian IOS

这篇文章主要介绍了IOS 照片编辑的view封装的实例详解的相关资料,这里提供实例帮助大家实现这样的功能,需要的朋友可以参考下

ios 照片编辑的view封装

该控件有旋转,缩放,拖动,剪裁的功能,封装成了一个imagecropperview类

需要导入的库:quartzcore.framework

imagecopperview.h

?
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
#import <uikit/uikit.h>
 
@protocol imagecropperdelegate;
 
@interface imagecropperview : uiview {
  uiimageview *imageview;
  
  id <imagecropperdelegate> delegate;
}
 
@property (nonatomic, retain) uiimage *image;
@property (nonatomic, retain) uiimage *croppedimage;
 
@property (nonatomic, assign) id <imagecropperdelegate> delegate;
 
@property (nonatomic, assign) bool enable;
@property (nonatomic, assign) bool ispaning;
 
- (void)setup;
- (void)finishcropping;
- (void)reset;
 
@end
 
@protocol imagecropperdelegate <nsobject>
- (void)changemovestatewithcropper:(uipangesturerecognizer*)gesture crop:(imagecropperview*)imagecrop;
@end

imagecopperview.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
#import "imagecropperview.h"
#import <quartzcore/quartzcore.h>
#include <math.h>
#import "uiimage+rotation.h"
 
@interface imagecropperview()
{
  @private
  cgsize _originalimageviewsize;
}
 
@property (nonatomic, retain) uiimageview *imageview;
@end
 
@implementation imagecropperview
 
@synthesize imageview, image = _image, delegate, croppedimage;
 
- (void)setup
{
  _enable = yes;
  self.clipstobounds = yes;
  self.backgroundcolor = [uicolor clearcolor];
  
  self.imageview = [[[uiimageview alloc] initwithframe:cgrectmake(0.0, 0.0, self.frame.size.width, self.frame.size.height)] autorelease];
  imageview.userinteractionenabled = yes;
  [self addsubview:imageview];
  
  uirotationgesturerecognizer *rotateges = [[uirotationgesturerecognizer alloc] initwithtarget:self action:@selector(rotateimage:)];
  [imageview addgesturerecognizer:rotateges];
  [rotateges release];
  
  uipinchgesturerecognizer *scaleges = [[uipinchgesturerecognizer alloc] initwithtarget:self action:@selector(scaleimage:)];
  [imageview addgesturerecognizer:scaleges];
  [scaleges release];
  
  uipangesturerecognizer *moveges = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(moveimage:)];
  [moveges setminimumnumberoftouches:1];
  [moveges setmaximumnumberoftouches:1];
  [imageview addgesturerecognizer:moveges];
  [moveges release];
}
 
- (id)initwithframe:(cgrect)frame {
  self = [super initwithframe:frame];
  
  if (self) {
    self.frame = frame;
    [self setup];
  }
  
  return self;
}
 
float _lasttransx = 0.0, _lasttransy = 0.0;
- (void)moveimage:(uipangesturerecognizer *)sender
{
  _ispaning = yes;
  if (delegate&&[delegate respondstoselector:@selector(changemovestatewithcropper:crop:)]) {
    [delegate changemovestatewithcropper:sender crop:self];
  }else{
    return;
  }
  if (sender.numberoftouches != 1||_enable == no) {
    return;
  }
  //获取在视图中手势的触点位置
  cgpoint translatedpoint = [sender translationinview:self];
 
  if([sender state] == uigesturerecognizerstatebegan) {
    _lasttransx = 0.0;
    _lasttransy = 0.0;
  }
  
  cgaffinetransform trans = cgaffinetransformmaketranslation(translatedpoint.x - _lasttransx, translatedpoint.y - _lasttransy);
  //cgaffinetransformconcat将imageview.transform和trans两个动画连续起来
  cgaffinetransform newtransform = cgaffinetransformconcat(imageview.transform, trans);
  _lasttransx = translatedpoint.x;
  _lasttransy = translatedpoint.y;
  nslog(@"_lasttransx==%f,_lasttransy==%f",_lasttransx,_lasttransy);
  imageview.transform = newtransform;
}
 
float _lastscale = 1.0;
- (void)scaleimage:(uipinchgesturerecognizer *)sender
{
  _ispaning = no;
  if (sender.numberoftouches != 2||_enable == no) {
    return;
  }
  
  if([sender state] == uigesturerecognizerstatebegan) {
    
    _lastscale = 1.0;
    return;
  }
  
  cgfloat scale = [sender scale]/_lastscale;
  
  cgaffinetransform currenttransform = imageview.transform;
  cgaffinetransform newtransform = cgaffinetransformscale(currenttransform, scale, scale);
  [imageview settransform:newtransform];
  
  _lastscale = [sender scale];
}
 
float _lastrotation = 0.0;
- (void)rotateimage:(uirotationgesturerecognizer *)sender
{
  _ispaning = no;
  if (sender.numberoftouches != 2||_enable == no) {
    return;
  }
  
  if([sender state] == uigesturerecognizerstateended) {
    
    _lastrotation = 0.0;
    return;
  }
  
  cgfloat rotation = -_lastrotation + [sender rotation];
  
  cgaffinetransform currenttransform = imageview.transform;
  cgaffinetransform newtransform = cgaffinetransformrotate(currenttransform,rotation);
  [imageview settransform:newtransform];
  
  _lastrotation = [sender rotation];
  
}
 
- (void)setimage:(uiimage *)image
{
  if (_image != image) {
    _image = [image retain];
  }
  
  float _imagescale = self.frame.size.width / image.size.width;
  self.imageview.frame = cgrectmake(0, 0, image.size.width*_imagescale, image.size.height*_imagescale);
  _originalimageviewsize = cgsizemake(image.size.width*_imagescale, image.size.height*_imagescale);
  imageview.image = image;
  imageview.center = cgpointmake(self.frame.size.width/2.0, self.frame.size.height/2.0);
}
 
- (void)finishcropping {
  float zoomscale = [[self.imageview.layer valueforkeypath:@"transform.scale.x"] floatvalue];
  float rotate = [[self.imageview.layer valueforkeypath:@"transform.rotation.z"] floatvalue];
  
  float _imagescale = _image.size.width/_originalimageviewsize.width;
  cgsize cropsize = cgsizemake(self.frame.size.width/zoomscale, self.frame.size.height/zoomscale);
  cgpoint croppervieworigin = cgpointmake((0.0 - self.imageview.frame.origin.x)/zoomscale,
                      (0.0 - self.imageview.frame.origin.y)/zoomscale);
  
  if((nsinteger)cropsize.width % 2 == 1)
  {
    cropsize.width = ceil(cropsize.width);
  }
  if((nsinteger)cropsize.height % 2 == 1)
  {
    cropsize.height = ceil(cropsize.height);
  }
  
  cgrect croprectinimage = cgrectmake((nsinteger)(croppervieworigin.x*_imagescale) ,(nsinteger)( croppervieworigin.y*_imagescale), (nsinteger)(cropsize.width*_imagescale),(nsinteger)(cropsize.height*_imagescale));
  
  uiimage *rotinputimage = [self.image imagerotatedbyradians:rotate];
  cgimageref tmp = cgimagecreatewithimageinrect([rotinputimage cgimage], croprectinimage);
  self.croppedimage = [uiimage imagewithcgimage:tmp scale:self.image.scale orientation:self.image.imageorientation];
  cgimagerelease(tmp);
}
 
- (void)reset
{
  self.imageview.transform = cgaffinetransformidentity;
}
 
- (void)dealloc {
  self.image = nil;
  self.croppedimage = nil;
  self.imageview = nil;
  
  [super dealloc];
}
 
@end

对uiimage添加了一个category

uiimage+rotation.h

?
1
2
3
4
5
6
7
8
#import <uikit/uikit.h>
 
@interface uiimage (rotation)
 
- (uiimage *)imagerotatedbyradians:(cgfloat)radians;
- (uiimage *)imagerotatedbydegrees:(cgfloat)degrees;
 
@end

uiimage+rotation.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
#import "uiimage+rotation.h"
 
/************
 角度=弧度/pi*180
 弧度=角度/180*pi
 *************/
 
cgfloat degreestoradians(cgfloat degrees) {return degrees * m_pi / 180;};
cgfloat radianstodegrees(cgfloat radians) {return radians * 180/m_pi;};
 
@implementation uiimage (rotation)
 
- (uiimage *)imagerotatedbyradians:(cgfloat)radians
{
  return [self imagerotatedbydegrees:radianstodegrees(radians)];
}
 
- (uiimage *)imagerotatedbydegrees:(cgfloat)degrees
{
  /*****
   cgaffinetransformmakerotation
   通过指定角度来创建一个旋转矩阵
   cgaffinetransformrotate
   在已存在的矩阵中使用旋转
   *****/
  uiview *rotatedviewbox = [[uiview alloc] initwithframe:cgrectmake(0,0,self.size.width, self.size.height)];
  cgaffinetransform t = cgaffinetransformmakerotation(degreestoradians(degrees));
  //给view旋转角度
  rotatedviewbox.transform = t;
  cgsize rotatedsize = rotatedviewbox.frame.size;
  [rotatedviewbox release];
  //开始编辑图形上下文
  uigraphicsbeginimagecontext(rotatedsize);
  //定义一个图形上下文
  cgcontextref bitmap = uigraphicsgetcurrentcontext();
  //沿x轴移动rotatedsize.width/2,y轴移动rotatedsize.height
  cgcontexttranslatectm(bitmap, rotatedsize.width/2, rotatedsize.height/2);
  //以原点(左下角)为中心旋转degreestoradians(degrees)弧度,正角度逆时针,负角度顺时针
  cgcontextrotatectm(bitmap, degreestoradians(degrees));
  //缩放x轴,y轴方向
  cgcontextscalectm(bitmap, 1.0, -1.0);
  //绘制位图
  cgcontextdrawimage(bitmap, cgrectmake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self cgimage]);
  //赋值给uiimage
  uiimage *resimage = uigraphicsgetimagefromcurrentimagecontext();
  //结束绘制
  uigraphicsendimagecontext();
  return resimage;
}
 
@end;

viewcontroller.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
#import "viewcontroller.h"
#import <quartzcore/quartzcore.h>
#import "imagecropperview.h"
 
@interface viewcontroller ()<imagecropperdelegate>{
}
 
@property (nonatomic, retain) iboutlet imagecropperview *cropper;
@property (nonatomic, retain) iboutlet uiimageview *result;
@property (retain, nonatomic) iboutlet uiimageview *resultsecond;
@property (nonatomic, retain) iboutlet uibutton *btn;
@property (retain, nonatomic) iboutlet imagecropperview *croppersecond;
@property (retain, nonatomic) iboutlet uibutton *cropbutton;
 
@end
 
@implementation viewcontroller
//@synthesize cropper, result, btn;
 
- (void)viewdidload
{
  [super viewdidload];
  // do any additional setup after loading the view, typically from a nib.
  _cropper.layer.borderwidth = 1.0;
  _cropper.layer.bordercolor = [uicolor bluecolor].cgcolor;
  _cropper.delegate = self;
  [_cropper setup];
  _cropper.image = [uiimage imagenamed:@"2.jpg"];
  [_btn addtarget:self action:@selector(buttonclicked) forcontrolevents:uicontroleventtouchupinside];
  
  _croppersecond.layer.bordercolor = [uicolor blackcolor].cgcolor;
  _croppersecond.layer.borderwidth = 2.0;
  _croppersecond.delegate = self;
  [_croppersecond setup];
  _croppersecond.image = [uiimage imagenamed:@"1.jpg"];
  [_cropbutton addtarget:self action:@selector(tapcropbutton) forcontrolevents:uicontroleventtouchupinside];
}
 
- (void)buttonclicked
{
  if ([_btn.currenttitle isequaltostring:@"crop1"]) {
    [_cropper finishcropping];//保存
    _result.image = _cropper.croppedimage;
    _cropper.hidden = yes;
    [_btn settitle:@"back" forstate:uicontrolstatenormal];
    [_btn settitle:@"back" forstate:uicontrolstatehighlighted];
  }else
  {
    [_cropper reset];
    _cropper.hidden = no;
    [_btn settitle:@"crop1" forstate:uicontrolstatenormal];
    [_btn settitle:@"crop1" forstate:uicontrolstatehighlighted];
    _result.image = nil;
  }
  _croppersecond.enable = yes;
  _cropper.enable = yes;
}
 
- (void)tapcropbutton{
  if ([_cropbutton.currenttitle isequaltostring:@"crop2"]) {
    [_croppersecond finishcropping];
    _croppersecond.enable = no;
    _resultsecond.image = _croppersecond.croppedimage;
    _croppersecond.hidden = yes;
    [_cropbutton settitle:@"back" forstate:uicontrolstatenormal];
    [_cropbutton settitle:@"back" forstate:uicontrolstatehighlighted];
  }else
  {
    [_croppersecond reset];
    
    _croppersecond.hidden = no;
    [_cropbutton settitle:@"crop2" forstate:uicontrolstatenormal];
    [_cropbutton settitle:@"crop2" forstate:uicontrolstatehighlighted];
    _resultsecond.image = nil;
  }
  _croppersecond.enable = yes;
  _cropper.enable = yes;
}
 
#pragma mark - imagecropperdelegate
- (void)changemovestatewithcropper:(uipangesturerecognizer*)gesture crop:(imagecropperview*)imagecrop{
  if (gesture.state == uigesturerecognizerstateended) {   
    nslog(@"点击编辑器结束,两个_cropper都可以进行编辑");
    _croppersecond.enable = yes;
    _cropper.enable = yes;
  }
}
 
 
- (void)touchesbegan:(nsset*)touches withevent:(uievent*)event;
{
//判断点击在控件上
  uitouch *touch = [touches anyobject];
  if ([_cropper pointinside:[touch locationinview:_cropper] withevent:nil]) {
    nslog(@"_cropper1 被触摸,禁用_cropper2");
    _croppersecond.enable = no;
  }else if ([_croppersecond pointinside:[touch locationinview:_croppersecond] withevent:nil]){
    nslog(@"_cropper2 被触摸,禁用_cropper1");
    _cropper.enable = no;
  }
}
 
- (void)dealloc {
  [_croppersecond release];
  [_cropbutton release];
  [_resultsecond release];
  [super dealloc];
}
- (void)viewdidunload {
  [self setcroppersecond:nil];
  [self setcropbutton:nil];
  [self setresultsecond:nil];
  [super viewdidunload];
}
@end

截图:

IOS 照片编辑的view封装的实例详解

最后要注意,因为我是用xib做的,拖上去的uiview要将其class改成imagecropperview

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://www.cnblogs.com/xiaobaizhu/archive/2013/07/03/3170101.html

延伸 · 阅读

精彩推荐
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03