版权所有,禁止匿名转载;禁止商业使用。
上传文件时,我们都的从本地中选择或用相机来拍摄得到文件。
一个上传按钮,单击事件
-(IBAction)btnClick{ UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"请选择文件来源" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"照相机",@"摄像机",@"本地相簿",@"本地视频",nil]; [actionSheet showInView:self.view]; [actionSheet release]; }
点击按钮触发的btnClick事件后将会弹出一个如下的选择筐:
接下来将要为UIActionSheet实现其中的委托了。
#pragma mark - #pragma UIActionSheet Delegate - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"buttonIndex = [%d]",buttonIndex); switch (buttonIndex) { case 0://照相机 {10 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = YES; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil]; [self presentModalViewController:imagePicker animated:YES]; [imagePicker release]; } break; case 1://摄像机 {22 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = YES; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil]; imagePicker.videoQuality = UIImagePickerControllerQualityTypeLow; [self presentModalViewController:imagePicker animated:YES]; [imagePicker release]; } break; case 2://本地相簿 {35 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = YES; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil]; [self presentModalViewController:imagePicker animated:YES]; [imagePicker release]; } break; case 3://本地视频 {47 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = YES; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil]; [self presentModalViewController:imagePicker animated:YES]; [imagePicker release]; } break; default: break; } }
实现了UIActionSheet的委托后,要发现,我们使用了UIImagePickerController,这个类将帮我们实现选取文件,打开对应的选取方式。比如当ButtonIndex为0的时候,它将帮我们打开照相机,我们可以使用相机拍摄照片作为上传的选取文件。因此,在这里我们还要实现UIImagePickerController的委托:
#pragma mark - #pragma UIImagePickerController Delegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeImage]) { UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage]; self.fileData = UIImageJPEGRepresentation(img, 1.0); } else if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeMovie]) { NSString *videoPath = [[info objectForKey:UIImagePickerControllerMediaURL] path]; self.fileData = [NSData dataWithContentsOfFile:videoPath]; } [picker dismissModalViewControllerAnimated:YES]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissModalViewControllerAnimated:YES]; 18 }
之后,你选取的文件便保存在了filedata中。就可以随时过来调用了。