1 图片选择合成器
1.1 问题
IOS设备提供了内置照相机和Photos应用程序,Photos应用程序可以帮助用户管理自己拍摄的各式照片和视频。应用程序通过图像选取器UIImagePickerController使用照相机和照片库,从特定源中选择图片的一种机制,可以用于图片也可以用于捕捉视频。本案例使用UIImagePickerController类实现一个图片选择合成器软件,可以从系统相册中挑选不同的图片,每张图片都可以进行放大缩小和移动,并且能将多张图片合成一张新的图片存入相册,如图-1所示:
图-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:方法中实现。
图-2
选择图片添加到ScrollView中呈现则需要在imagePickerController: didFinishPickingMediaWithInfo:方法中实现,并且本案例还可以将选择的图片从ScrollView中删除,因此每张ScrollView中的图片还带有一个删除按钮,如图-3所示:
图-3
最后返回图片合成界面,将从系统相册选择的图片展示在presentScrollView上面,给self.view添加长按手势,当长按某张图片时表示用户选择该图片进行编辑,生成一个可以拖拽的图片副本,然后结合前面所学的手势和变形再self.collection界面中实现对图片拖拽、放大、缩小等操作。
1.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:搭建界面
首先创建一个SingleViewApplication应用,在Storyboard文件中搭建图片合成器的界面,上方是一个ScrollView控件用于展示用户从相册选择的图片,当用户在该区域长按某张图片时界面会出现一个可以拖拽的图片副本,用户可以将该图片拖拽到编辑区,将该控件关联成TRViewController的输出口属性presentScrollView。
界面中间有一个View控件用于显示合成图片,在该控件内可以拖拽缩放旋转用户所选择的图片,下方是两个按钮,一个按钮用于弹出ImagePickerController,另一个按钮用于合成图片,分别将View控件关联成TRViewController的属性collection,将按钮关联成同一个方法click:,通过按钮的tag值进行区别不同的用户需求。
在Storyboard中完成的界面如图-4所示:
图-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所示:
图-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所示:
图-6
图-7
1.4 完整代码
本案例中,TRViewController.h文件中的完整代码如下所示:
- #import <UIKit/UIKit.h>
- @interface TRViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
- @property (nonatomic,strong) NSMutableArray *images;
- @end
本案例中,TRViewController.m文件中的完整代码如下所示:
- #import "TRViewController.h"
- #import "TRImagePickerController.h"
- @implementation UIImage (Scale)
- -(UIImage *)TransformtoSize:(CGSize)Newsize
- {
- // 创建一个bitmap的context
- UIGraphicsBeginImageContext(Newsize);
- // 绘制改变大小的图片
- [self drawInRect:CGRectMake(0, 0, Newsize.width, Newsize.height)];
- // 从当前context中创建一个改变大小后的图片
- UIImage *TransformedImg=UIGraphicsGetImageFromCurrentImageContext();
- // 使当前的context出堆栈
- UIGraphicsEndImageContext();
- // 返回新的改变大小后的图片
- return TransformedImg;
- }
- @end
- @interface TRViewController ()<TRImagePickerControllerDelegate,UIGestureRecognizerDelegate>
- @property (weak, nonatomic) IBOutlet UIScrollView *presentScrollView;
- @property (weak, nonatomic) IBOutlet UIView *collection;
- @property (nonatomic,strong) UIImageView *dragIV;
- @end
- @implementation TRViewController
- -(NSMutableArray *)images {
- if (!_images) {
- _images = [NSMutableArray array];
- }
- return _images;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
- [self.view addGestureRecognizer:longPress];
- }
- -(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)];
- }
- }
- -(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;
- }
- - (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;
- }
- }
- - (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];
- }
- }
- -(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;
- }
- }
- @end
本案例中,TRImagePickerController.h文件中的完整代码如下所示:
- #import <UIKit/UIKit.h>
- #import "TRViewController.h"
- @class TRImagePickerController;
- @protocol TRImagePickerControllerDelegate <NSObject>
- -(void)addPickedImageViewWithImages:(NSMutableArray*)images;
- @end
- @interface TRImagePickerController : UIImagePickerController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
- @property (nonatomic,assign) id<TRImagePickerControllerDelegate> viewDelegate;
- @end
本案例中,TRImagePickerController.m文件中的完整代码如下所示:
- #import "TRImagePickerController.h"
- @interface TRImagePickerController ()
- @property (nonatomic,strong) UIScrollView *pickerScrollView;
- @property (nonatomic,strong)NSMutableArray *images;
- @property (nonatomic,strong)NSMutableArray *imageViews;
- @end
- @implementation TRImagePickerController
- -(NSMutableArray *)images {
- if (!_images) {
- _images = [NSMutableArray array];
- }
- return _images;
- }
- -(NSMutableArray *)imageViews {
- if (!_imageViews) {
- _imageViews = [NSMutableArray array];
- }
- return _imageViews;
- }
- -(void)viewDidLoad {
- [super viewDidLoad];
- self.delegate = self;
- }
- -(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];
- }
- - (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)];
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
- -(void)finishPickImage {
- [self dismissViewControllerAnimated:YES completion:nil];
- [self.viewDelegate addPickedImageViewWithImages:self.images];
- }
- @end