IOS打开照相机与本地相册选择图片

最近正好项目里面要集成“打开照相机与本地相册选择图片”的功能,今天就在这边给大家写一个演示程序;打开相机拍摄后或者在相册中选择一张照片,然后将它显示在界面上。好了废话不多说,因为比较简单直接上源码。

首先,我们在头文件中添加需要用到的actionSheet控件,显示图片的UIImageView控件,并且加上所需要的协议

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ImagePickerViewController : UIViewController<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>  
  4.   
  5. @property (strongnonatomic) IBOutlet UIImageView *headImage;  
  6.   
  7. @property (strongnonatomicUIActionSheet *actionSheet;  
  8.   
  9. - (IBAction)clickPickImage:(id)sender;  
  10. @end  

通过点击我设置在界面中的按钮来呼出actionSheet控件,来选择相应的操作拍照或是在相册中选择相片,代码如下:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  ImagePickerViewController.m  
  3. //  testAuto  
  4. //  
  5. //  Created by silicon on 15/5/9.  
  6. //  Copyright (c) 2015年 silicon. All rights reserved.  
  7. //  
  8.   
  9. #import "ImagePickerViewController.h"  
  10.   
  11. @interface ImagePickerViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation ImagePickerViewController  
  16.   
  17. @synthesize actionSheet = _actionSheet;  
  18.   
  19. - (void)viewDidLoad {  
  20.     [super viewDidLoad];  
  21.     // Do any additional setup after loading the view from its nib.  
  22.       
  23. }  
  24.   
  25. - (void)didReceiveMemoryWarning {  
  26.     [super didReceiveMemoryWarning];  
  27.     // Dispose of any resources that can be recreated.  
  28. }  
  29.   
  30.   
  31. /** 
  32.  @ 调用ActionSheet 
  33.  */  
  34. - (void)callActionSheetFunc{  
  35.     if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){  
  36.         self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照"@"从相册选择", nil nil];  
  37.     }else{  
  38.         self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil nil];  
  39.     }  
  40.       
  41.     self.actionSheet.tag = 1000;  
  42.     [self.actionSheet showInView:self.view];  
  43. }  
  44.   
  45. // Called when a button is clicked. The view will be automatically dismissed after this call returns  
  46. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{  
  47.     if (actionSheet.tag == 1000) {  
  48.         NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  49.         // 判断是否支持相机  
  50.         if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {  
  51.             switch (buttonIndex) {  
  52.                 case 0:  
  53.                     //来源:相机  
  54.                     sourceType = UIImagePickerControllerSourceTypeCamera;  
  55.                     break;  
  56.                 case 1:  
  57.                     //来源:相册  
  58.                     sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  59.                     break;  
  60.                 case 2:  
  61.                     return;  
  62.             }  
  63.         }  
  64.         else {  
  65.             if (buttonIndex == 2) {  
  66.                 return;  
  67.             } else {  
  68.                 sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;  
  69.             }  
  70.         }  
  71.         // 跳转到相机或相册页面  
  72.         UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];  
  73.         imagePickerController.delegate = self;  
  74.         imagePickerController.allowsEditing = YES;  
  75.         imagePickerController.sourceType = sourceType;  
  76.           
  77.         [self presentViewController:imagePickerController animated:YES completion:^{  
  78.           
  79.         }];  
  80.     }  
  81. }  
  82.   
  83. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  
  84. {  
  85.     [picker dismissViewControllerAnimated:YES completion:^{  
  86.       
  87.     }];  
  88.       
  89.     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];  
  90.     self.headImage.image = image;  
  91. }  
  92.   
  93. /* 
  94. #pragma mark - Navigation 
  95.  
  96. // In a storyboard-based application, you will often want to do a little preparation before navigation 
  97. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
  98.     // Get the new view controller using [segue destinationViewController]. 
  99.     // Pass the selected object to the new view controller. 
  100. } 
  101. */  
  102.   
  103. - (IBAction)clickPickImage:(id)sender {  
  104.       
  105.     [self callActionSheetFunc];  
  106. }  
  107. @end  

代码比较简单,也容易理解,运行的效果如下:

















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值