1 图片选择合成器
1.1 问题
IOS设备提供了内置照相机和Photos应用程序,Photos应用程序可以帮助用户管理自己拍摄的各式照片和视频。应用程序通过图像选取器UIImagePickerController使用照相机和照片库,从特定源中选择图片的一种机制,可以用于图片也可以用于捕捉视频。本案例使用UIImagePickerController类实现一个图片选择合成器软件,可以从系统相册中挑选不同的图片,每张图片都可以进行放大缩小和移动,并且能将多张图片合成一张新的图片存入相册,如图-1所示:
![](http://pdf7.tarena.com.cn/tts8_source/ttsPage/IOS/ISD_V01/MEDIA-SYSSERVICES/DAY04/CASE/01/index.files/image001.png)
图-1
1.2 方案
首先创建一个SingleViewApplication应用,在Storyboard文件中搭建图片合成器的界面,上方是一个ScrollView控件用于展示用户从相册选择的图片,当用户在该区域长按某张图片时界面会出现一个可以拖拽的图片副本,用户可以将该图片拖拽到编辑区,将该控件关联成TRViewController的输出口属性presentScrollView。
界面中间有一个View控件用于显示合成图片,在该控件内可以拖拽缩放旋转用户所选择的图片,下方是两个按钮,一个按钮用于弹出ImagePickerController,另一个按钮用于合成图片,分别将View控件关联成TRViewController的属性collection,将按钮关联成同一个方法click:,通过按钮的tag值进行区别不同的用户需求。
其次创建一个TRImageViewController继承至UIImageViewController,该类就是图片选择控制器,需要遵守UINavigationControllerDelegate协议和UIImagePickerControllerDelegate协议,主要通过实现协议方法完成访问系统相册,挑选图片等功能。
挑选照片的界面如图-2所示,挑选的图片将呈现在下方的ScrollView上面,因此需要在弹出ImagePickerController时创建一个ScrollView和一个确定按钮,当点击确定按钮时表示用户完成图片选择,返回之前的界面,该功能需要在navigationController:didShowViewController:animated:方法中实现。
![](http://pdf7.tarena.com.cn/tts8_source/ttsPage/IOS/ISD_V01/MEDIA-SYSSERVICES/DAY04/CASE/01/index.files/image002.png)
图-2
选择图片添加到ScrollView中呈现则需要在imagePickerController: didFinishPickingMediaWithInfo:方法中实现,并且本案例还可以将选择的图片从ScrollView中删除,因此每张ScrollView中的图片还带有一个删除按钮,如图-3所示:
![](http://pdf7.tarena.com.cn/tts8_source/ttsPage/IOS/ISD_V01/MEDIA-SYSSERVICES/DAY04/CASE/01/index.files/image003.png)
图-3
最后返回图片合成界面,将从系统相册选择的图片展示在presentScrollView上面,给self.view添加长按手势,当长按某张图片时表示用户选择该图片进行编辑,生成一个可以拖拽的图片副本,然后结合前面所学的手势和变形再self.collection界面中实现对图片拖拽、放大、缩小等操作。
1.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:搭建界面
首先创建一个SingleViewApplication应用,在Storyboard文件中搭建图片合成器的界面,上方是一个ScrollView控件用于展示用户从相册选择的图片,当用户在该区域长按某张图片时界面会出现一个可以拖拽的图片副本,用户可以将该图片拖拽到编辑区,将该控件关联成TRViewController的输出口属性presentScrollView。
界面中间有一个View控件用于显示合成图片,在该控件内可以拖拽缩放旋转用户所选择的图片,下方是两个按钮,一个按钮用于弹出ImagePickerController,另一个按钮用于合成图片,分别将View控件关联成TRViewController的属性collection,将按钮关联成同一个方法click:,通过按钮的tag值进行区别不同的用户需求。
在Storyboard中完成的界面如图-4所示:
![](http://pdf7.tarena.com.cn/tts8_source/ttsPage/IOS/ISD_V01/MEDIA-SYSSERVICES/DAY04/CASE/01/index.files/image004.png)
图-4
步骤二:创建TRImagePickerController类
首先创建一个TRImageViewController继承至UIImageViewController,该类就是图片选择控制器,需要遵守UINavigationControllerDelegate协议和UIImagePickerControllerDelegate协议,主要通过实现协议方法完成访问系统相册,挑选图片等功能。当用户点击选择图片按钮时就会弹出一个该类的实例,根据所需定义如下属性:
UIScrollView类型的pickerScrollView,用于呈现用户选择的图片;
NSMutableArray类型的images,用于存储用户选择的原图片;
NSMutableArray类型的imageViews,用于存储展示在pickerScrollView上面的图片,代码如下所示:
@interface TRImagePickerController ()
@property (nonatomic,strong) UIScrollView *pickerScrollView;
@property (nonatomic,strong)NSMutableArray *images;
@property (nonatomic,strong)NSMutableArray *imageViews;
@end
用户所挑选的图片将呈现在下方的ScrollView上面,因此需要在弹出ImagePickerController时创建一个ScrollView和一个确定按钮,当点击确定按钮时表示用户完成图片选择,返回之前的界面,该功能需要在navigationController:didShowViewController:animated:方法中实现,代码如下所示:
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//创建确定按钮
UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 450, 320, 20)];
[iv setBackgroundColor:[UIColor yellowColor]];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(270, 0, 50, 20);
[btn setTitle:@"确定" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(finishPickImage) forControlEvents:UIControlEventTouchUpInside];
btn.tag = 1;
[iv addSubview:btn];
iv.userInteractionEnabled = YES;
[viewController.view addSubview:iv];
//创建呈现挑选图片的ScrollView
self.pickerScrollView = [[UIScrollView alloc]init];
self.pickerScrollView.frame = CGRectMake(0, 470, 320,80);
[self.pickerScrollView setBackgroundColor:[UIColor grayColor]];
[viewController.view addSubview:self.pickerScrollView];
}
然后当用户完成图片选择时,将所选择图片添加到ScrollView中呈现则需要在imagePickerController: didFinishPickingMediaWithInfo:方法中实现,并且本案例还可以将选择的图片从ScrollView中删除,因此每张ScrollView中的图片还带有一个删除按钮,代码如下所示:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"%@",info);
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[self.images addObject:image];
UIImageView *iv = [[UIImageView alloc]initWithImage:image];
iv.frame = CGRectMake(self.imageViews.count*80, 0, 80, 80);
iv.userInteractionEnabled = YES;
[self.imageViews addObject:iv];
//添加删除按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(58, 0, 20, 20);
[btn setTitle:@"X" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(deletePicked:) forControlEvents:UIControlEventTouchUpInside];
[iv addSubview:btn];
//添加到挑选图片的ScrollView中
[self.pickerScrollView addSubview:iv];
[self.pickerScrollView setContentSize:CGSizeMake(self.imageViews.count*80,self.pickerScrollView.frame.size.height)];
}
接下来实现删除图片的方法,删除图片时除了要从界面移除,还需要从存储图片的数组中移除,然后重新呈现图片,代码如下所示:
- (void)deletePicked:(UIButton*)btn {
UIImageView *deleteImageView = (UIImageView*)btn.superview;
[self.images removeObject:deleteImageView.image];
[deleteImageView removeFromSuperview];
[self.imageViews removeObject:deleteImageView];
for (UIImageView *iv in self.imageViews) {
[iv removeFromSuperview];
}
for (int i=0;i<self.imageViews.count;i++) {
UIImageView *iv = self.imageViews[i];
iv.frame = CGRectMake(i*80, 0, 78, 88);
[self.pickerScrollView addSubview:iv];
}
[self.pickerScrollView setContentSize:CGSizeMake(self.imageViews.count*80, 90)];
}
最后点击确定按钮完成图片选择,退出当前图片选择控制器,回退到图片合成界面,但是需要将用户选择的图片信息传递到图片合成界面,这里反向传值采用委托的方式,代码如下所示:
//TRViewController.h文件里面定义协议和委托属性
@class TRImagePickerController;
@protocol TRImagePickerControllerDelegate <NSObject>
-(void)addPickedImageViewWithImages:(NSMutableArray*)images;
@end
@interface TRImagePickerController : UIImagePickerController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic,assign) id<TRImagePickerControllerDelegate> viewDelegate;
@end
//TRViewController.m文件里面传值
-(void)finishPickImage {
[self dismissViewControllerAnimated:YES completion:nil];
[self.viewDelegate addPickedImageViewWithImages:self.images];
}
步骤三:完成图片合成界面
当返回图片合成界面,通过实现协议方法将从系统相册选择的图片展示在presentScrollView上面,代码如下所示:
-(void)addPickedImageViewWithImages:(NSMutableArray *)images{
self.images = images;
if (self.images.count>0) {
NSLog(@"%d",self.images.count);
for (int i = 0; i<self.images.count; i++) {
UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(i*80, 0, 80,80)];
iv.image = self.images[i];
iv.userInteractionEnabled = YES;
iv.multipleTouchEnabled = YES;
[self.presentScrollView addSubview:iv];
}
[self.presentScrollView setContentSize:CGSizeMake(80*self.images.count, 80)];
}
}
- (UIImage*)getImageFromView:(UIView*)view {
UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
if (error) {
NSLog(@"err = %@",[error localizedDescription]);
}else {
UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"提示" message:@"保存成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[av show];
}
}
完成效果如图-5所示:
![](http://pdf7.tarena.com.cn/tts8_source/ttsPage/IOS/ISD_V01/MEDIA-SYSSERVICES/DAY04/CASE/01/index.files/image005.png)
图-5
然后实现两个按钮的动作方click:,通过tag值进行区分,为0则挑选图片,为1则合成图片,代码如下所示:
- (IBAction)click:(UIButton *)sender {
switch (sender.tag) {
case 0:
{
TRImagePickerController *ipc = [[TRImagePickerController alloc]init];
[ipc setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
ipc.viewDelegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
break;
case 1:
{
UIImage *image = [[self getImageFromView:self.collection] TransformtoSize:CGSizeMake(self.collection.frame.size.width*10, self.collection.frame.size.height*10)];
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
}
break;
}
}
最后在viewDidload方法中给self.view添加长按手势,识别用户选择的图片,生成一个可以拖拽的图片副本,当拖拽到编辑区时将图片添加到编辑区,并且给self.dragIVd添加移动缩放旋转等手势,代码如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[self.view addGestureRecognizer:longPress];
}
//长按手势方法
-(void)longPress:(UILongPressGestureRecognizer*)longPress {
CGPoint point = [longPress locationInView:longPress.view];
if (!self.dragIV) {
self.dragIV = [[UIImageView alloc]init];
}
switch ((int)longPress.state) {
case UIGestureRecognizerStateBegan:
{
CGPoint newPoint = [self.presentScrollView convertPoint:point fromView:self.view];
if(self.images.count>0){
for (UIImageView *iv in self.presentScrollView.subviews) {
if (CGRectContainsPoint(iv.frame, newPoint)) {
self.dragIV.image = iv.image;
self.dragIV.frame = iv.frame;
}
}
self.dragIV.center = point;
[self.view addSubview:self.dragIV];
}
}
break;
case UIGestureRecognizerStateChanged:
{
if (self.dragIV) {
self.dragIV.center = point;
}
}
break;
case UIGestureRecognizerStateEnded:
{
if (CGRectContainsRect(self.collection.frame, self.dragIV.frame)) {
CGPoint newPoint = [self.collection convertPoint:point fromView:self.view];
self.dragIV.center = newPoint;
self.dragIV.userInteractionEnabled = YES;
self.dragIV.multipleTouchEnabled = YES;
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
pinch.delegate = self;
[self.dragIV addGestureRecognizer:pinch];
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
rotation.delegate = self;
[self.dragIV addGestureRecognizer:rotation];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
pan.delegate = self;
[self.dragIV addGestureRecognizer:pan];
[self.collection addSubview:self.dragIV];
self.dragIV = nil;
}else {
[self.dragIV removeFromSuperview];
}
}
break;
}
}
-(void)panAction:(UIPanGestureRecognizer*)sender {
UIImageView *iv = (UIImageView*)sender.view;
iv.center = [sender locationInView:self.collection];
}
-(void)rotationAction:(UIRotationGestureRecognizer*)sender {
UIImageView *iv = (UIImageView*)sender.view;
CGAffineTransform transform = iv.transform;
transform = CGAffineTransformRotate(transform, sender.rotation);
iv.transform = transform;
sender.rotation = 0;
}
-(void)pinchAction:(UIPinchGestureRecognizer*)sender {
UIImageView *iv = (UIImageView*)sender.view;
CGAffineTransform transform = iv.transform;
transform = CGAffineTransformScale(transform, sender.scale, sender.scale);
iv.transform = transform;
sender.scale = 1;
}
//同时识别手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
最后完成效果如图-6,图-7所示:
![](http://pdf7.tarena.com.cn/tts8_source/ttsPage/IOS/ISD_V01/MEDIA-SYSSERVICES/DAY04/CASE/01/index.files/image006.png)
图-6
![](http://pdf7.tarena.com.cn/tts8_source/ttsPage/IOS/ISD_V01/MEDIA-SYSSERVICES/DAY04/CASE/01/index.files/image007.png)
图-7
1 1.4 完整代码
2
3 本案例中,TRViewController.h文件中的完整代码如下所示:
4
5 #import <UIKit/UIKit.h>
6 @interface TRViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
7 @property (nonatomic,strong) NSMutableArray *images;
8 @end
9
10 本案例中,TRViewController.m文件中的完整代码如下所示:
11
12
13 #import "TRViewController.h"
14 #import "TRImagePickerController.h"
15 @implementation UIImage (Scale)
16 -(UIImage *)TransformtoSize:(CGSize)Newsize
17 {
18 // 创建一个bitmap的context
19 UIGraphicsBeginImageContext(Newsize);
20 // 绘制改变大小的图片
21 [self drawInRect:CGRectMake(0, 0, Newsize.width, Newsize.height)];
22 // 从当前context中创建一个改变大小后的图片
23 UIImage *TransformedImg=UIGraphicsGetImageFromCurrentImageContext();
24 // 使当前的context出堆栈
25 UIGraphicsEndImageContext();
26 // 返回新的改变大小后的图片
27 return TransformedImg;
28 }
29 @end
30 @interface TRViewController ()<TRImagePickerControllerDelegate,UIGestureRecognizerDelegate>
31 @property (weak, nonatomic) IBOutlet UIScrollView *presentScrollView;
32 @property (weak, nonatomic) IBOutlet UIView *collection;
33 @property (nonatomic,strong) UIImageView *dragIV;
34 @end
35 @implementation TRViewController
36 -(NSMutableArray *)images {
37 if (!_images) {
38 _images = [NSMutableArray array];
39 }
40 return _images;
41 }
42 - (void)viewDidLoad
43 {
44 [super viewDidLoad];
45 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
46 [self.view addGestureRecognizer:longPress];
47 }
48 -(void)addPickedImageViewWithImages:(NSMutableArray *)images{
49 self.images = images;
50 if (self.images.count>0) {
51 NSLog(@"%d",self.images.count);
52 for (int i = 0; i<self.images.count; i++) {
53 UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(i*80, 0, 80,80)];
54 iv.image = self.images[i];
55 iv.userInteractionEnabled = YES;
56 iv.multipleTouchEnabled = YES;
57 [self.presentScrollView addSubview:iv];
58 }
59 [self.presentScrollView setContentSize:CGSizeMake(80*self.images.count, 80)];
60 }
61 }
62 -(void)panAction:(UIPanGestureRecognizer*)sender {
63 UIImageView *iv = (UIImageView*)sender.view;
64 iv.center = [sender locationInView:self.collection];
65 }
66 -(void)rotationAction:(UIRotationGestureRecognizer*)sender {
67 UIImageView *iv = (UIImageView*)sender.view;
68 CGAffineTransform transform = iv.transform;
69 transform = CGAffineTransformRotate(transform, sender.rotation);
70 iv.transform = transform;
71 sender.rotation = 0;
72 }
73 -(void)pinchAction:(UIPinchGestureRecognizer*)sender {
74 UIImageView *iv = (UIImageView*)sender.view;
75 CGAffineTransform transform = iv.transform;
76 transform = CGAffineTransformScale(transform, sender.scale, sender.scale);
77 iv.transform = transform;
78 sender.scale = 1;
79 }
80 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
81 return YES;
82 }
83 - (IBAction)click:(UIButton *)sender {
84 switch (sender.tag) {
85 case 0:
86 {
87 TRImagePickerController *ipc = [[TRImagePickerController alloc]init];
88 [ipc setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
89 ipc.viewDelegate = self;
90 [self presentViewController:ipc animated:YES completion:nil];
91 }
92 break;
93 case 1:
94 {
95 UIImage *image = [[self getImageFromView:self.collection] TransformtoSize:CGSizeMake(self.collection.frame.size.width*10, self.collection.frame.size.height*10)];
96 UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
97 }
98 break;
99 }
100 }
101 - (UIImage*)getImageFromView:(UIView*)view {
102 UIGraphicsBeginImageContext(view.frame.size);
103 [view.layer renderInContext:UIGraphicsGetCurrentContext()];
104 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
105 UIGraphicsEndImageContext();
106 return image;
107 }
108 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
109 if (error) {
110 NSLog(@"err = %@",[error localizedDescription]);
111 }else {
112 UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"提示" message:@"保存成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
113 [av show];
114 }
115 }
116 -(void)longPress:(UILongPressGestureRecognizer*)longPress {
117 CGPoint point = [longPress locationInView:longPress.view];
118 if (!self.dragIV) {
119 self.dragIV = [[UIImageView alloc]init];
120 }
121 switch ((int)longPress.state) {
122 case UIGestureRecognizerStateBegan:
123 {
124 CGPoint newPoint = [self.presentScrollView convertPoint:point fromView:self.view];
125 if(self.images.count>0){
126 for (UIImageView *iv in self.presentScrollView.subviews) {
127 if (CGRectContainsPoint(iv.frame, newPoint)) {
128 self.dragIV.image = iv.image;
129 self.dragIV.frame = iv.frame;
130 }
131 }
132 self.dragIV.center = point;
133 [self.view addSubview:self.dragIV];
134 }
135 }
136 break;
137 case UIGestureRecognizerStateChanged:
138 {
139 if (self.dragIV) {
140 self.dragIV.center = point;
141 }
142 }
143 break;
144 case UIGestureRecognizerStateEnded:
145 {
146 if (CGRectContainsRect(self.collection.frame, self.dragIV.frame)) {
147 CGPoint newPoint = [self.collection convertPoint:point fromView:self.view];
148 self.dragIV.center = newPoint;
149 self.dragIV.userInteractionEnabled = YES;
150 self.dragIV.multipleTouchEnabled = YES;
151 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
152 pinch.delegate = self;
153 [self.dragIV addGestureRecognizer:pinch];
154 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
155 rotation.delegate = self;
156 [self.dragIV addGestureRecognizer:rotation];
157 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
158 pan.delegate = self;
159 [self.dragIV addGestureRecognizer:pan];
160 [self.collection addSubview:self.dragIV];
161 self.dragIV = nil;
162 }else {
163 [self.dragIV removeFromSuperview];
164 }
165 }
166 break;
167 }
168 }
169 @end
170
171 本案例中,TRImagePickerController.h文件中的完整代码如下所示:
172
173 #import <UIKit/UIKit.h>
174 #import "TRViewController.h"
175 @class TRImagePickerController;
176 @protocol TRImagePickerControllerDelegate <NSObject>
177 -(void)addPickedImageViewWithImages:(NSMutableArray*)images;
178 @end
179 @interface TRImagePickerController : UIImagePickerController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
180 @property (nonatomic,assign) id<TRImagePickerControllerDelegate> viewDelegate;
181 @end
182
183 本案例中,TRImagePickerController.m文件中的完整代码如下所示:
184
185 #import "TRImagePickerController.h"
186 @interface TRImagePickerController ()
187 @property (nonatomic,strong) UIScrollView *pickerScrollView;
188 @property (nonatomic,strong)NSMutableArray *images;
189 @property (nonatomic,strong)NSMutableArray *imageViews;
190 @end
191 @implementation TRImagePickerController
192 -(NSMutableArray *)images {
193 if (!_images) {
194 _images = [NSMutableArray array];
195 }
196 return _images;
197 }
198 -(NSMutableArray *)imageViews {
199 if (!_imageViews) {
200 _imageViews = [NSMutableArray array];
201 }
202 return _imageViews;
203 }
204 -(void)viewDidLoad {
205 [super viewDidLoad];
206 self.delegate = self;
207 }
208 -(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
209 //创建确定按钮
210 UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 450, 320, 20)];
211 [iv setBackgroundColor:[UIColor yellowColor]];
212 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
213 btn.frame = CGRectMake(270, 0, 50, 20);
214 [btn setTitle:@"确定" forState:UIControlStateNormal];
215 [btn addTarget:self action:@selector(finishPickImage) forControlEvents:UIControlEventTouchUpInside];
216 btn.tag = 1;
217 [iv addSubview:btn];
218 iv.userInteractionEnabled = YES;
219 [viewController.view addSubview:iv];
220 //创建呈现挑选图片的ScrollView
221 self.pickerScrollView = [[UIScrollView alloc]init];
222 self.pickerScrollView.frame = CGRectMake(0, 470, 320,80);
223 [self.pickerScrollView setBackgroundColor:[UIColor grayColor]];
224 [viewController.view addSubview:self.pickerScrollView];
225 }
226 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
227 NSLog(@"%@",info);
228 UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
229 [self.images addObject:image];
230 UIImageView *iv = [[UIImageView alloc]initWithImage:image];
231 iv.frame = CGRectMake(self.imageViews.count*80, 0, 80, 80);
232 iv.userInteractionEnabled = YES;
233 [self.imageViews addObject:iv];
234 //添加删除按钮
235 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
236 btn.frame = CGRectMake(58, 0, 20, 20);
237 [btn setTitle:@"X" forState:UIControlStateNormal];
238 [btn addTarget:self action:@selector(deletePicked:) forControlEvents:UIControlEventTouchUpInside];
239 [iv addSubview:btn];
240 //添加到挑选图片的ScrollView中
241 [self.pickerScrollView addSubview:iv];
242 [self.pickerScrollView setContentSize:CGSizeMake(self.imageViews.count*80,self.pickerScrollView.frame.size.height)];
243 }
244 - (void)deletePicked:(UIButton*)btn {
245 UIImageView *deleteImageView = (UIImageView*)btn.superview;
246 [self.images removeObject:deleteImageView.image];
247 [deleteImageView removeFromSuperview];
248 [self.imageViews removeObject:deleteImageView];
249 for (UIImageView *iv in self.imageViews) {
250 [iv removeFromSuperview];
251 }
252 for (int i=0;i<self.imageViews.count;i++) {
253 UIImageView *iv = self.imageViews[i];
254 iv.frame = CGRectMake(i*80, 0, 78, 88);
255 [self.pickerScrollView addSubview:iv];
256 }
257 [self.pickerScrollView setContentSize:CGSizeMake(self.imageViews.count*80, 90)];
258 }
259 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
260 [picker dismissViewControllerAnimated:YES completion:nil];
261 }
262 -(void)finishPickImage {
263 [self dismissViewControllerAnimated:YES completion:nil];
264 [self.viewDelegate addPickedImageViewWithImages:self.images];
265 }
266 @end