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 }

點擊按鈕觸發的btnClick事件后將會彈出一個如下的選擇筐:

接下來將要為UIActionSheet實現其中的委托了。

 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 }

實現了UIActionSheet的委托后,要發現,我們使用了UIImagePickerController,這個類將幫我們實現選取文件,打開對應的選取方式。比如當ButtonIndex為0的時候,它將幫我們打開照相機,我們可以使用相機拍攝照片作為上傳的選取文件。因此,在這里我們還要實現UIImagePickerController的委托:

 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 }

之后,你選取的文件便保存在了filedata中。就可以隨時過來調用了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM