码迷,mamicode.com
首页 > 其他好文 > 详细

照片和音频 、 系统邮件、短信

时间:2015-12-16 21:14:06      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

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上面的图片,代码如下所示:

  1. @interface TRImagePickerController ()
  2. @property (nonatomic,strong) UIScrollView *pickerScrollView;
  3. @property (nonatomic,strong)NSMutableArray *images;
  4. @property (nonatomic,strong)NSMutableArray *imageViews;
  5. @end

用户所挑选的图片将呈现在下方的ScrollView上面,因此需要在弹出ImagePickerController时创建一个ScrollView和一个确定按钮,当点击确定按钮时表示用户完成图片选择,返回之前的界面,该功能需要在navigationController:didShowViewController:animated:方法中实现,代码如下所示:

  1. -(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  2. //创建确定按钮
  3. UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 450, 320, 20)];
  4. [iv setBackgroundColor:[UIColor yellowColor]];
  5. UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  6. btn.frame = CGRectMake(270, 0, 50, 20);
  7. [btn setTitle:@"确定" forState:UIControlStateNormal];
  8. [btn addTarget:self action:@selector(finishPickImage) forControlEvents:UIControlEventTouchUpInside];
  9. btn.tag = 1;
  10. [iv addSubview:btn];
  11. iv.userInteractionEnabled = YES;
  12. [viewController.view addSubview:iv];
  13. //创建呈现挑选图片的ScrollView
  14. self.pickerScrollView = [[UIScrollView alloc]init];
  15. self.pickerScrollView.frame = CGRectMake(0, 470, 320,80);
  16. [self.pickerScrollView setBackgroundColor:[UIColor grayColor]];
  17. [viewController.view addSubview:self.pickerScrollView];
  18. }

然后当用户完成图片选择时,将所选择图片添加到ScrollView中呈现则需要在imagePickerController: didFinishPickingMediaWithInfo:方法中实现,并且本案例还可以将选择的图片从ScrollView中删除,因此每张ScrollView中的图片还带有一个删除按钮,代码如下所示:

  1. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  2. NSLog(@"%@",info);
  3. UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  4. [self.images addObject:image];
  5. UIImageView *iv = [[UIImageView alloc]initWithImage:image];
  6. iv.frame = CGRectMake(self.imageViews.count*80, 0, 80, 80);
  7. iv.userInteractionEnabled = YES;
  8. [self.imageViews addObject:iv];
  9. //添加删除按钮
  10. UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  11. btn.frame = CGRectMake(58, 0, 20, 20);
  12. [btn setTitle:@"X" forState:UIControlStateNormal];
  13. [btn addTarget:self action:@selector(deletePicked:) forControlEvents:UIControlEventTouchUpInside];
  14. [iv addSubview:btn];
  15. //添加到挑选图片的ScrollView中
  16. [self.pickerScrollView addSubview:iv];
  17. [self.pickerScrollView setContentSize:CGSizeMake(self.imageViews.count*80,self.pickerScrollView.frame.size.height)];
  18. }

接下来实现删除图片的方法,删除图片时除了要从界面移除,还需要从存储图片的数组中移除,然后重新呈现图片,代码如下所示:

  1. - (void)deletePicked:(UIButton*)btn {
  2. UIImageView *deleteImageView = (UIImageView*)btn.superview;
  3. [self.images removeObject:deleteImageView.image];
  4. [deleteImageView removeFromSuperview];
  5. [self.imageViews removeObject:deleteImageView];
  6. for (UIImageView *iv in self.imageViews) {
  7. [iv removeFromSuperview];
  8. }
  9. for (int i=0;i<self.imageViews.count;i++) {
  10. UIImageView *iv = self.imageViews[i];
  11. iv.frame = CGRectMake(i*80, 0, 78, 88);
  12. [self.pickerScrollView addSubview:iv];
  13. }
  14. [self.pickerScrollView setContentSize:CGSizeMake(self.imageViews.count*80, 90)];
  15. }

最后点击确定按钮完成图片选择,退出当前图片选择控制器,回退到图片合成界面,但是需要将用户选择的图片信息传递到图片合成界面,这里反向传值采用委托的方式,代码如下所示:

  1. //TRViewController.h文件里面定义协议和委托属性
  2. @class TRImagePickerController;
  3. @protocol TRImagePickerControllerDelegate <NSObject>
  4. -(void)addPickedImageViewWithImages:(NSMutableArray*)images;
  5. @end
  6. @interface TRImagePickerController : UIImagePickerController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
  7. @property (nonatomic,assign) id<TRImagePickerControllerDelegate> viewDelegate;
  8. @end
  9. //TRViewController.m文件里面传值
  10. -(void)finishPickImage {
  11. [self dismissViewControllerAnimated:YES completion:nil];
  12. [self.viewDelegate addPickedImageViewWithImages:self.images];
  13. }

步骤三:完成图片合成界面

当返回图片合成界面,通过实现协议方法将从系统相册选择的图片展示在presentScrollView上面,代码如下所示:

  1. -(void)addPickedImageViewWithImages:(NSMutableArray *)images{
  2. self.images = images;
  3. if (self.images.count>0) {
  4. NSLog(@"%d",self.images.count);
  5. for (int i = 0; i<self.images.count; i++) {
  6. UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(i*80, 0, 80,80)];
  7. iv.image = self.images[i];
  8. iv.userInteractionEnabled = YES;
  9. iv.multipleTouchEnabled = YES;
  10. [self.presentScrollView addSubview:iv];
  11. }
  12. [self.presentScrollView setContentSize:CGSizeMake(80*self.images.count, 80)];
  13. }
  14. }
  15. - (UIImage*)getImageFromView:(UIView*)view {
  16. UIGraphicsBeginImageContext(view.frame.size);
  17. [view.layer renderInContext:UIGraphicsGetCurrentContext()];
  18. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  19. UIGraphicsEndImageContext();
  20. return image;
  21. }
  22. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
  23. if (error) {
  24. NSLog(@"err = %@",[error localizedDescription]);
  25. }else {
  26. UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"提示" message:@"保存成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  27. [av show];
  28. }
  29. }

完成效果如图-5所示:

技术分享

图-5

然后实现两个按钮的动作方click:,通过tag值进行区分,为0则挑选图片,为1则合成图片,代码如下所示:

  1. - (IBAction)click:(UIButton *)sender {
  2. switch (sender.tag) {
  3. case 0:
  4. {
  5. TRImagePickerController *ipc = [[TRImagePickerController alloc]init];
  6. [ipc setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
  7. ipc.viewDelegate = self;
  8. [self presentViewController:ipc animated:YES completion:nil];
  9. }
  10. break;
  11. case 1:
  12. {
  13. UIImage *image = [[self getImageFromView:self.collection] TransformtoSize:CGSizeMake(self.collection.frame.size.width*10, self.collection.frame.size.height*10)];
  14. UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
  15. }
  16. break;
  17. }
  18. }

最后在viewDidload方法中给self.view添加长按手势,识别用户选择的图片,生成一个可以拖拽的图片副本,当拖拽到编辑区时将图片添加到编辑区,并且给self.dragIVd添加移动缩放旋转等手势,代码如下所示:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
  5. [self.view addGestureRecognizer:longPress];
  6. }
  7. //长按手势方法
  8. -(void)longPress:(UILongPressGestureRecognizer*)longPress {
  9. CGPoint point = [longPress locationInView:longPress.view];
  10. if (!self.dragIV) {
  11. self.dragIV = [[UIImageView alloc]init];
  12. }
  13. switch ((int)longPress.state) {
  14. case UIGestureRecognizerStateBegan:
  15. {
  16. CGPoint newPoint = [self.presentScrollView convertPoint:point fromView:self.view];
  17. if(self.images.count>0){
  18. for (UIImageView *iv in self.presentScrollView.subviews) {
  19. if (CGRectContainsPoint(iv.frame, newPoint)) {
  20. self.dragIV.image = iv.image;
  21. self.dragIV.frame = iv.frame;
  22. }
  23. }
  24. self.dragIV.center = point;
  25. [self.view addSubview:self.dragIV];
  26. }
  27. }
  28. break;
  29. case UIGestureRecognizerStateChanged:
  30. {
  31. if (self.dragIV) {
  32. self.dragIV.center = point;
  33. }
  34. }
  35. break;
  36. case UIGestureRecognizerStateEnded:
  37. {
  38. if (CGRectContainsRect(self.collection.frame, self.dragIV.frame)) {
  39. CGPoint newPoint = [self.collection convertPoint:point fromView:self.view];
  40. self.dragIV.center = newPoint;
  41. self.dragIV.userInteractionEnabled = YES;
  42. self.dragIV.multipleTouchEnabled = YES;
  43. UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
  44. pinch.delegate = self;
  45. [self.dragIV addGestureRecognizer:pinch];
  46. UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
  47. rotation.delegate = self;
  48. [self.dragIV addGestureRecognizer:rotation];
  49. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
  50. pan.delegate = self;
  51. [self.dragIV addGestureRecognizer:pan];
  52. [self.collection addSubview:self.dragIV];
  53. self.dragIV = nil;
  54. }else {
  55. [self.dragIV removeFromSuperview];
  56. }
  57. }
  58. break;
  59. }
  60. }
  61. -(void)panAction:(UIPanGestureRecognizer*)sender {
  62. UIImageView *iv = (UIImageView*)sender.view;
  63. iv.center = [sender locationInView:self.collection];
  64. }
  65. -(void)rotationAction:(UIRotationGestureRecognizer*)sender {
  66. UIImageView *iv = (UIImageView*)sender.view;
  67. CGAffineTransform transform = iv.transform;
  68. transform = CGAffineTransformRotate(transform, sender.rotation);
  69. iv.transform = transform;
  70. sender.rotation = 0;
  71. }
  72. -(void)pinchAction:(UIPinchGestureRecognizer*)sender {
  73. UIImageView *iv = (UIImageView*)sender.view;
  74. CGAffineTransform transform = iv.transform;
  75. transform = CGAffineTransformScale(transform, sender.scale, sender.scale);
  76. iv.transform = transform;
  77. sender.scale = 1;
  78. }
  79. //同时识别手势
  80. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  81. return YES;
  82. }

最后完成效果如图-6,图-7所示:

技术分享

图-6

技术分享

图-7

1.4 完整代码

本案例中,TRViewController.h文件中的完整代码如下所示:

  1. #import <UIKit/UIKit.h>
  2. @interface TRViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
  3. @property (nonatomic,strong) NSMutableArray *images;
  4. @end
 

本案例中,TRViewController.m文件中的完整代码如下所示:

 
  1. #import "TRViewController.h"
  2. #import "TRImagePickerController.h"
  3. @implementation UIImage (Scale)
  4. -(UIImage *)TransformtoSize:(CGSize)Newsize
  5. {
  6. // 创建一个bitmap的context
  7. UIGraphicsBeginImageContext(Newsize);
  8. // 绘制改变大小的图片
  9. [self drawInRect:CGRectMake(0, 0, Newsize.width, Newsize.height)];
  10. // 从当前context中创建一个改变大小后的图片
  11. UIImage *TransformedImg=UIGraphicsGetImageFromCurrentImageContext();
  12. // 使当前的context出堆栈
  13. UIGraphicsEndImageContext();
  14. // 返回新的改变大小后的图片
  15. return TransformedImg;
  16. }
  17. @end
  18. @interface TRViewController ()<TRImagePickerControllerDelegate,UIGestureRecognizerDelegate>
  19. @property (weak, nonatomic) IBOutlet UIScrollView *presentScrollView;
  20. @property (weak, nonatomic) IBOutlet UIView *collection;
  21. @property (nonatomic,strong) UIImageView *dragIV;
  22. @end
  23. @implementation TRViewController
  24. -(NSMutableArray *)images {
  25. if (!_images) {
  26. _images = [NSMutableArray array];
  27. }
  28. return _images;
  29. }
  30. - (void)viewDidLoad
  31. {
  32. [super viewDidLoad];
  33. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
  34. [self.view addGestureRecognizer:longPress];
  35. }
  36. -(void)addPickedImageViewWithImages:(NSMutableArray *)images{
  37. self.images = images;
  38. if (self.images.count>0) {
  39. NSLog(@"%d",self.images.count);
  40. for (int i = 0; i<self.images.count; i++) {
  41. UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(i*80, 0, 80,80)];
  42. iv.image = self.images[i];
  43. iv.userInteractionEnabled = YES;
  44. iv.multipleTouchEnabled = YES;
  45. [self.presentScrollView addSubview:iv];
  46. }
  47. [self.presentScrollView setContentSize:CGSizeMake(80*self.images.count, 80)];
  48. }
  49. }
  50. -(void)panAction:(UIPanGestureRecognizer*)sender {
  51. UIImageView *iv = (UIImageView*)sender.view;
  52. iv.center = [sender locationInView:self.collection];
  53. }
  54. -(void)rotationAction:(UIRotationGestureRecognizer*)sender {
  55. UIImageView *iv = (UIImageView*)sender.view;
  56. CGAffineTransform transform = iv.transform;
  57. transform = CGAffineTransformRotate(transform, sender.rotation);
  58. iv.transform = transform;
  59. sender.rotation = 0;
  60. }
  61. -(void)pinchAction:(UIPinchGestureRecognizer*)sender {
  62. UIImageView *iv = (UIImageView*)sender.view;
  63. CGAffineTransform transform = iv.transform;
  64. transform = CGAffineTransformScale(transform, sender.scale, sender.scale);
  65. iv.transform = transform;
  66. sender.scale = 1;
  67. }
  68. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  69. return YES;
  70. }
  71. - (IBAction)click:(UIButton *)sender {
  72. switch (sender.tag) {
  73. case 0:
  74. {
  75. TRImagePickerController *ipc = [[TRImagePickerController alloc]init];
  76. [ipc setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
  77. ipc.viewDelegate = self;
  78. [self presentViewController:ipc animated:YES completion:nil];
  79. }
  80. break;
  81. case 1:
  82. {
  83. UIImage *image = [[self getImageFromView:self.collection] TransformtoSize:CGSizeMake(self.collection.frame.size.width*10, self.collection.frame.size.height*10)];
  84. UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
  85. }
  86. break;
  87. }
  88. }
  89. - (UIImage*)getImageFromView:(UIView*)view {
  90. UIGraphicsBeginImageContext(view.frame.size);
  91. [view.layer renderInContext:UIGraphicsGetCurrentContext()];
  92. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  93. UIGraphicsEndImageContext();
  94. return image;
  95. }
  96. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
  97. if (error) {
  98. NSLog(@"err = %@",[error localizedDescription]);
  99. }else {
  100. UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"提示" message:@"保存成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  101. [av show];
  102. }
  103. }
  104. -(void)longPress:(UILongPressGestureRecognizer*)longPress {
  105. CGPoint point = [longPress locationInView:longPress.view];
  106. if (!self.dragIV) {
  107. self.dragIV = [[UIImageView alloc]init];
  108. }
  109. switch ((int)longPress.state) {
  110. case UIGestureRecognizerStateBegan:
  111. {
  112. CGPoint newPoint = [self.presentScrollView convertPoint:point fromView:self.view];
  113. if(self.images.count>0){
  114. for (UIImageView *iv in self.presentScrollView.subviews) {
  115. if (CGRectContainsPoint(iv.frame, newPoint)) {
  116. self.dragIV.image = iv.image;
  117. self.dragIV.frame = iv.frame;
  118. }
  119. }
  120. self.dragIV.center = point;
  121. [self.view addSubview:self.dragIV];
  122. }
  123. }
  124. break;
  125. case UIGestureRecognizerStateChanged:
  126. {
  127. if (self.dragIV) {
  128. self.dragIV.center = point;
  129. }
  130. }
  131. break;
  132. case UIGestureRecognizerStateEnded:
  133. {
  134. if (CGRectContainsRect(self.collection.frame, self.dragIV.frame)) {
  135. CGPoint newPoint = [self.collection convertPoint:point fromView:self.view];
  136. self.dragIV.center = newPoint;
  137. self.dragIV.userInteractionEnabled = YES;
  138. self.dragIV.multipleTouchEnabled = YES;
  139. UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
  140. pinch.delegate = self;
  141. [self.dragIV addGestureRecognizer:pinch];
  142. UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
  143. rotation.delegate = self;
  144. [self.dragIV addGestureRecognizer:rotation];
  145. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
  146. pan.delegate = self;
  147. [self.dragIV addGestureRecognizer:pan];
  148. [self.collection addSubview:self.dragIV];
  149. self.dragIV = nil;
  150. }else {
  151. [self.dragIV removeFromSuperview];
  152. }
  153. }
  154. break;
  155. }
  156. }
  157. @end
 

本案例中,TRImagePickerController.h文件中的完整代码如下所示:

  1. #import <UIKit/UIKit.h>
  2. #import "TRViewController.h"
  3. @class TRImagePickerController;
  4. @protocol TRImagePickerControllerDelegate <NSObject>
  5. -(void)addPickedImageViewWithImages:(NSMutableArray*)images;
  6. @end
  7. @interface TRImagePickerController : UIImagePickerController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
  8. @property (nonatomic,assign) id<TRImagePickerControllerDelegate> viewDelegate;
  9. @end
 

本案例中,TRImagePickerController.m文件中的完整代码如下所示:

  1. #import "TRImagePickerController.h"
  2. @interface TRImagePickerController ()
  3. @property (nonatomic,strong) UIScrollView *pickerScrollView;
  4. @property (nonatomic,strong)NSMutableArray *images;
  5. @property (nonatomic,strong)NSMutableArray *imageViews;
  6. @end
  7. @implementation TRImagePickerController
  8. -(NSMutableArray *)images {
  9. if (!_images) {
  10. _images = [NSMutableArray array];
  11. }
  12. return _images;
  13. }
  14. -(NSMutableArray *)imageViews {
  15. if (!_imageViews) {
  16. _imageViews = [NSMutableArray array];
  17. }
  18. return _imageViews;
  19. }
  20. -(void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.delegate = self;
  23. }
  24. -(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  25. //创建确定按钮
  26. UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 450, 320, 20)];
  27. [iv setBackgroundColor:[UIColor yellowColor]];
  28. UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  29. btn.frame = CGRectMake(270, 0, 50, 20);
  30. [btn setTitle:@"确定" forState:UIControlStateNormal];
  31. [btn addTarget:self action:@selector(finishPickImage) forControlEvents:UIControlEventTouchUpInside];
  32. btn.tag = 1;
  33. [iv addSubview:btn];
  34. iv.userInteractionEnabled = YES;
  35. [viewController.view addSubview:iv];
  36. //创建呈现挑选图片的ScrollView
  37. self.pickerScrollView = [[UIScrollView alloc]init];
  38. self.pickerScrollView.frame = CGRectMake(0, 470, 320,80);
  39. [self.pickerScrollView setBackgroundColor:[UIColor grayColor]];
  40. [viewController.view addSubview:self.pickerScrollView];
  41. }
  42. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  43. NSLog(@"%@",info);
  44. UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  45. [self.images addObject:image];
  46. UIImageView *iv = [[UIImageView alloc]initWithImage:image];
  47. iv.frame = CGRectMake(self.imageViews.count*80, 0, 80, 80);
  48. iv.userInteractionEnabled = YES;
  49. [self.imageViews addObject:iv];
  50. //添加删除按钮
  51. UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  52. btn.frame = CGRectMake(58, 0, 20, 20);
  53. [btn setTitle:@"X" forState:UIControlStateNormal];
  54. [btn addTarget:self action:@selector(deletePicked:) forControlEvents:UIControlEventTouchUpInside];
  55. [iv addSubview:btn];
  56. //添加到挑选图片的ScrollView中
  57. [self.pickerScrollView addSubview:iv];
  58. [self.pickerScrollView setContentSize:CGSizeMake(self.imageViews.count*80,self.pickerScrollView.frame.size.height)];
  59. }
  60. - (void)deletePicked:(UIButton*)btn {
  61. UIImageView *deleteImageView = (UIImageView*)btn.superview;
  62. [self.images removeObject:deleteImageView.image];
  63. [deleteImageView removeFromSuperview];
  64. [self.imageViews removeObject:deleteImageView];
  65. for (UIImageView *iv in self.imageViews) {
  66. [iv removeFromSuperview];
  67. }
  68. for (int i=0;i<self.imageViews.count;i++) {
  69. UIImageView *iv = self.imageViews[i];
  70. iv.frame = CGRectMake(i*80, 0, 78, 88);
  71. [self.pickerScrollView addSubview:iv];
  72. }
  73. [self.pickerScrollView setContentSize:CGSizeMake(self.imageViews.count*80, 90)];
  74. }
  75. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  76. [picker dismissViewControllerAnimated:YES completion:nil];
  77. }
  78. -(void)finishPickImage {
  79. [self dismissViewControllerAnimated:YES completion:nil];
  80. [self.viewDelegate addPickedImageViewWithImages:self.images];
  81. }
  82. @end

照片和音频 、 系统邮件、短信

标签:

原文地址:http://www.cnblogs.com/52190112cn/p/5052131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!