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

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
				
时间: 2024-12-30 03:08:17

照片和音频 、 系统邮件、短信的相关文章

iOS 之(调用系统发送短信功能)

今天处理一个订单详情的界面,在订单详情页面上有联系方式(电话号码),在电话号码的右边有两图片,一个是电话样式的,一个是短信样式的,做为一名程序员(号称攻城师)一眼看下去就应该是一个 button,而不是 imageView ,既然是 button 那就应该是打电话和发短信的,这样一来的话,当我们点击短信的这个 button 时,就应该调用系统发短信的界面,下面先来看看效果图与需求:       这篇随笔主要是记录总结发短信的代码,打电话迟点更新 下面来看调用系统发短信的实现代码 老规矩,新建一个

IOS问题汇总:2015-1-9 iOS 调用系统发短信以及打电话功能

iOS 调用系统发短信以及打电话功能 ios电话smsinterface互联网class先介绍一种最简单的方法: 调用打电话功能 [[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@“tel://10086”]]; 调用发短信功能 [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@“sms://10000”]]; 上面的发短信的功能是调用系统的

iOS调用系统发短信功能详解

iOS调用系统的发短信功能可以分为两种:1,程序外调用系统发短信.2,程序内调用系统发短信.第二种的好处是用户发短信之后还可以回到app.这对app来说非常重要. 程序外调用系统发短信 这个方法其实很简单,直接调用openURL即可: [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://13888888888"]]; 程序内调用系统发短信 1)导入MessageUI.framework,并引入头文

IOS 调用系统发短信以及打电话的功能

IOS 调用系统发短信以及打电话的功能 http://blog.csdn.net/lwq421336220/article/details/7818979 先介绍一种最简单的方法: 调用打电话功能 [[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]]; 调用发短信功能 [[UIApplication sharedApplication]openURL:[NSURL URLWithS

zabbix配置及邮件短信报警

Zabbix邮件报警的大致流程如下: 监控主机---->监控项---->触发器---->告警动作---->告警方式---->告警用户 首先添加监控项 1.添加受监控项的主机 组态---主机---右上角"创建主机" 弹出主机创建页面 上图填完后先不要点存档,而是点击左上角的模板,选择要使用的模板(可选择多个模板,一个模板中包含一个或多个监控项目),也可以在"模板"选项中创建一个新的模板,选择完模板后点添加,再点 存档 在主机列表中点击被监

调用系统发短信的功能

来自为知笔记(Wiz)

调用系统的打电话,发短信,邮件,蓝牙

在开发某些应用时可能希望能够调用iOS系统内置的电话.短信.邮件.浏览器应用,此时你可以直接使用UIApplication的OpenURL:方法指定特定的协议来打开不同的系统应用.常用的协议如下: 打电话:tel:或者tel://.telprompt:或telprompt://(拨打电话前有提示) 发短信:sms:或者sms:// 发送邮件:mailto:或者mailto:// 启动浏览器:http:或者http:// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

IOS中调用系统的电话、短信、邮件、浏览功能

iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 2015-01-13 09:16 by KenshinCui, 26990 阅读, 35 评论, 收藏, 编辑 --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用系统应用.使用系统服务: 调用系统应用 使用系统服务目 录 短信与邮件 通讯录

android调用系统的分享功能实现分享到短信,邮件和其他

之前写过一篇文章利用友盟等第三方实现分享到微信,微博和豆瓣等第三方社交站点.本次我们利用系统自带的分享功能去做,虽然简单,但对于以前类似友盟等第三方社交分享组件还没做的成熟的时候,也是一种好的选择.所以这里直接上代码对系统分享做个记录,代码有必要注释,所以就不做啥说明了: /** * 弹出分享列表 */ private void showShareDialog(){ AlertDialog.Builder builder = new AlertDialog.Builder(Thread_Cont

iOS应用调用系统打电话、发短信和发邮件功能

摘要: 在应用程序内,调用系统的功能来实现打电话.发短信和发邮件,通过电话号码或者邮箱,直接跳转到系统的功能界面. PS:调试好像只能真机调试,模拟器没有反应,真机就可以跳转,不知道是不是必须真机,但方法肯定是可行的. 1.打电话 应用内调用系统打电话有两种方式: 1)WebView方式 使用WebView来跳转,把电话号码通过URL传递给WebView,这种方式会弹出是否要拨打的提示,可以选择拨打或者不拨打,打完也会自动回到应用界面,推荐. UIWebView *callWebview =[[