上傳檔案時,我們都的從本地中選擇或用相機來拍攝得到檔案。
一個上傳按鈕,單擊事件
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中。就可以隨時過來調用了。