IOS打开照相机与本地相册选择图片实例详解

时间:2022-09-20 12:00:39

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

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

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

?
1
2
3
4
5
6
7
8
9
10
#import <uikit/uikit.h>
 
@interface imagepickerviewcontroller : uiviewcontroller<uiimagepickercontrollerdelegate,uiactionsheetdelegate,uinavigationcontrollerdelegate>
 
@property (strong, nonatomic) iboutlet uiimageview *headimage;
 
@property (strong, nonatomic) uiactionsheet *actionsheet;
 
- (ibaction)clickpickimage:(id)sender;
@end

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// imagepickerviewcontroller.m
// testauto
//
// created by silicon on 15/5/9.
// copyright (c) 2015年 silicon. all rights reserved.
//
 
#import "imagepickerviewcontroller.h"
 
@interface imagepickerviewcontroller ()
 
@end
 
@implementation imagepickerviewcontroller
 
@synthesize actionsheet = _actionsheet;
 
- (void)viewdidload {
  [super viewdidload];
  // do any additional setup after loading the view from its nib.
   
}
 
- (void)didreceivememorywarning {
  [super didreceivememorywarning];
  // dispose of any resources that can be recreated.
}
 
 
/**
 @ 调用actionsheet
 */
- (void)callactionsheetfunc{
  if([uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypecamera]){
    self.actionsheet = [[uiactionsheet alloc] initwithtitle:@"选择图像" delegate:self cancelbuttontitle:@"取消" destructivebuttontitle:nil otherbuttontitles:@"拍照", @"从相册选择", nil nil];
  }else{
    self.actionsheet = [[uiactionsheet alloc] initwithtitle:@"选择图像" delegate:self cancelbuttontitle:@"取消"destructivebuttontitle:nil otherbuttontitles:@"从相册选择", nil nil];
  }
   
  self.actionsheet.tag = 1000;
  [self.actionsheet showinview:self.view];
}
 
// called when a button is clicked. the view will be automatically dismissed after this call returns
- (void)actionsheet:(uiactionsheet *)actionsheet clickedbuttonatindex:(nsinteger)buttonindex{
  if (actionsheet.tag == 1000) {
    nsuinteger sourcetype = uiimagepickercontrollersourcetypephotolibrary;
    // 判断是否支持相机
    if([uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {
      switch (buttonindex) {
        case 0:
          //来源:相机
          sourcetype = uiimagepickercontrollersourcetypecamera;
          break;
        case 1:
          //来源:相册
          sourcetype = uiimagepickercontrollersourcetypephotolibrary;
          break;
        case 2:
          return;
      }
    }
    else {
      if (buttonindex == 2) {
        return;
      } else {
        sourcetype = uiimagepickercontrollersourcetypesavedphotosalbum;
      }
    }
    // 跳转到相机或相册页面
    uiimagepickercontroller *imagepickercontroller = [[uiimagepickercontroller alloc] init];
    imagepickercontroller.delegate = self;
    imagepickercontroller.allowsediting = yes;
    imagepickercontroller.sourcetype = sourcetype;
     
    [self presentviewcontroller:imagepickercontroller animated:yes completion:^{
     
    }];
  }
}
 
- (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info
{
  [picker dismissviewcontrolleranimated:yes completion:^{
   
  }];
   
  uiimage *image = [info objectforkey:uiimagepickercontrolleroriginalimage];
  self.headimage.image = image;
}
 
/*
#pragma mark - navigation
 
// in a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender {
  // get the new view controller using [segue destinationviewcontroller].
  // pass the selected object to the new view controller.
}
*/
 
- (ibaction)clickpickimage:(id)sender {
   
  [self callactionsheetfunc];
}
@end

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

IOS打开照相机与本地相册选择图片实例详解

IOS打开照相机与本地相册选择图片实例详解

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/shenjie12345678/article/details/45645477