IOS获取摄像和本地中的资源

上传文件时,我们都的从本地中选择或用相机来拍摄得到文件。

一个上传按钮,单击事件

IOS获取摄像和本地中的资源
 1 -(IBAction)btnClick{
2 UIActionSheet* actionSheet = [[UIActionSheet alloc]
3 initWithTitle:@"请选择文件来源"
4 delegate:self
5 cancelButtonTitle:@"取消"
6 destructiveButtonTitle:nil
7 otherButtonTitles:@"照相机",@"摄像机",@"本地相簿",@"本地视频",nil];
8 [actionSheet showInView:self.view];
9 [actionSheet release];
10 }
IOS获取摄像和本地中的资源

点击按钮触发的btnClick事件后将会弹出一个如下的选择筐:

IOS获取摄像和本地中的资源

接下来将要为UIActionSheet实现其中的委托了。

IOS获取摄像和本地中的资源
 1 #pragma mark -
2 #pragma UIActionSheet Delegate
3 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
4 {
5 NSLog(@"buttonIndex = [%d]",buttonIndex);
6 switch (buttonIndex) {
7 case 0://照相机
8 {10 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
11 imagePicker.delegate = self;
12 imagePicker.allowsEditing = YES;
13 imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
14 imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
15 [self presentModalViewController:imagePicker animated:YES];
16 [imagePicker release];
17 }
18 break;
19 case 1://摄像机
20 {22 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
23 imagePicker.delegate = self;
24 imagePicker.allowsEditing = YES;
25 imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
26 imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
27 imagePicker.videoQuality = UIImagePickerControllerQualityTypeLow;
28 [self presentModalViewController:imagePicker animated:YES];
29 [imagePicker release];
30 }
31 break;
32 case 2://本地相簿
33 {35 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
36 imagePicker.delegate = self;
37 imagePicker.allowsEditing = YES;
38 imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
39 imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
40 [self presentModalViewController:imagePicker animated:YES];
41 [imagePicker release];
42 }
43 break;
44 case 3://本地视频
45 {47 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
48 imagePicker.delegate = self;
49 imagePicker.allowsEditing = YES;
50 imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
51 imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
52 [self presentModalViewController:imagePicker animated:YES];
53 [imagePicker release];
54 }
55 break;
56 default:
57 break;
58 }
59 }
IOS获取摄像和本地中的资源

实现了UIActionSheet的委托后,要发现,我们使用了UIImagePickerController,这个类将帮我们实现选取文件,打开对应的选取方式。比如当ButtonIndex为0的时候,它将帮我们打开照相机,我们可以使用相机拍摄照片作为上传的选取文件。因此,在这里我们还要实现UIImagePickerController的委托:

IOS获取摄像和本地中的资源
 1 #pragma mark -
2 #pragma UIImagePickerController Delegate
3 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
4 {
5 if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeImage]) {
6 UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
7 self.fileData = UIImageJPEGRepresentation(img, 1.0);
8 } else if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeMovie]) {
9 NSString *videoPath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
10 self.fileData = [NSData dataWithContentsOfFile:videoPath];
11 }
12 [picker dismissModalViewControllerAnimated:YES];
13 }
14
15 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
16 {
17 [picker dismissModalViewControllerAnimated:YES];
18 }
IOS获取摄像和本地中的资源

之后,你选取的文件便保存在了filedata中。就可以随时过来调用了。

上一篇:【leetcode】Intersection of Two Linked Lists


下一篇:[leetCode][003] Intersection of Two Linked Lists