记得添加MobileCoreServices.framework
及导入#import <MobileCoreServices/MobileCoreServices.h>
1 @interface PPViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>
2
3 {
4
5 UIImagePickerController *_pickerController;
6
7 }
8
9 @property (weak, nonatomic) IBOutlet UIImageView *imageView;
10
11
12
13 @end
14
15
16
17 @implementation PPViewController
18
19
20
21 - (void)viewDidLoad
22
23 {
24
25 [super viewDidLoad];
26
27 _pickerController = [[UIImagePickerController alloc] init];
28
29 _pickerController.delegate = self;//设置代理
30
31 _pickerController.allowsEditing = YES;//图片可编辑(放大缩小)
32
33 }
34
35 - (IBAction)choseImage:(id)sender
36
37 {
38
39 // 判断是否有相机
40
41 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
42
43 {
44
45
46
47 //判断是否能摄像
48
49 if ([[UIImagePickerController availableMediaTypesForSourceType:_pickerController.sourceType] containsObject:(NSString *)kUTTypeMovie])
50
51 {
52
53 _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
54
55 UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"相机",@"摄像机", nil];
56
57 sheet.tag = 2;
58
59 [sheet showInView:self.view];
60
61 }
62
63 else
64
65 {
66
67 UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"相册", nil];
68
69 sheet.tag = 1;
70
71 [sheet showInView:self.view];
72
73 }
74
75 }
76
77
78
79 else
80
81 {//模拟器
82
83 UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"相册", nil];
84
85 sheet.tag = 3;
86
87 [sheet showInView:self.view];
88
89 }
90
91
92
93 }
94
95 #pragma mark- UIActionSheet的代理方法
96
97 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
98
99 {
100
101 if (actionSheet.tag == 1)
102
103 {
104
105 switch (buttonIndex)
106
107 {
108
109 case 0:
110
111 {
112
113 _pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
114
115 _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
116
117 [self presentViewController:_pickerController animated:YES completion:nil];
118
119 }
120
121 break;
122
123 case 1:
124
125 {
126
127 _pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
128
129 _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
130
131 [self presentViewController:_pickerController animated:YES completion:nil];
132
133 }
134
135 break;
136
137
138
139 default:
140
141 break;
142
143 }
144
145
146
147 }
148
149 if (actionSheet.tag == 2)
150
151 {
152
153 switch (buttonIndex)
154
155 {
156
157 case 0:
158
159 {
160
161 _pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
162
163 _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
164
165 [self presentViewController:_pickerController animated:YES completion:nil];
166
167 }
168
169 break;
170
171 case 1:
172
173 {
174
175 _pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
176
177 // _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
178
179 [self presentViewController:_pickerController animated:YES completion:nil];
180
181 }
182
183 break;
184
185 case 2:
186
187 {
188
189 _pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
190
191 // pickController.videoQuality = UIImagePickerControllerQualityTypeLow;//可以选择图片质量
192
193 _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
194
195 [self presentViewController:_pickerController animated:YES completion:nil];
196
197 }
198
199 default:
200
201 break;
202
203 }
204
205 }
206
207 if (actionSheet.tag == 3)
208
209 {
210
211 if (buttonIndex == 0)
212
213 {
214
215 _pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
216
217 // _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
218
219 [self presentViewController:_pickerController animated:YES completion:nil];
220
221 }
222
223
224
225 }
226
227 }
228
229
230
231 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
232
233 {
234
235 NSLog(@"========%@",info);//返回的值都在info里面
236
237 if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeMovie])
238
239 {
240
241 _imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:(NSString *)[[info objectForKey:UIImagePickerControllerMediaURL] path]]];
242
243 }
244
245 else
246
247 {
248
249 _imageView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
250
251 }
252
253 [self dismissViewControllerAnimated:YES completion:nil];
254
255
256
257 }
258
259 //如果这是一个modalViewController,需要dismiss 它发了个cancel消息,必须关闭它
260
261 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
262
263 {
264
265 [self dismissViewControllerAnimated:YES completion:nil];
266
267 }
268
269 /**
270
271 * {
272
273 UIImagePickerControllerMediaType = "public.image";
274
275 UIImagePickerControllerOriginalImage = "<UIImage: 0x7fe3a3dc2a20> size {1500, 1001} orientation 0 scale 1.000000";
276
277 UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=B6C0A21C-07C3-493D-8B44-3BA4C9981C25&ext=JPG";
278
279 }
280
281 */
282
283 /**
284
285 * {
286
287 UIImagePickerControllerCropRect = "NSRect: {{0, 0}, {1500, 1003}}";
288
289 UIImagePickerControllerEditedImage = "<UIImage: 0x7fec5c8145b0> size {638, 426} orientation 0 scale 1.000000";
290
291 UIImagePickerControllerMediaType = "public.image";
292
293 UIImagePickerControllerOriginalImage = "<UIImage: 0x7fec5c80d4c0> size {1500, 1001} orientation 0 scale 1.000000";
294
295 UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=B6C0A21C-07C3-493D-8B44-3BA4C9981C25&ext=JPG";
296
297 }
298
299 */