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中。就可以随时过来调用了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp是一个跨平台的应用开发框架,可以同时开发出安卓、iOS和H5等多个平台的应用。在Uniapp中,我们可以使用一些插件来实现H5手机上传图片视频的功能。 对于图片上传,可以使用uni.uploadFile方法,通过选择图片后,将其以formData的形式上传到后端服务器。代码示例如下: ``` uni.chooseImage({ success: function (res) { uni.uploadFile({ url: 'http://example.com/upload', filePath: res.tempFilePaths[0], name: 'file', success: function (res) { console.log('图片上传成功'); }, fail: function (err) { console.error('图片上传失败', err); } }); } }); ``` 而对于视频上传,可以使用uni.chooseVideo方法选择要上传视频文件,然后使用uni.uploadFile方法将其上传到后端服务器。代码示例如下: ``` uni.chooseVideo({ success: function (res) { uni.uploadFile({ url: 'http://example.com/upload', filePath: res.tempFilePath, name: 'file', success: function (res) { console.log('视频上传成功'); }, fail: function (err) { console.error('视频上传失败', err); } }); } }); ``` 需要注意的是,上传文件需要后端服务器的支持,我们需要提前配置好后端接口来处理文件上传的请求。另外,在使用上传功能之前,需要先在uni-app的manifest.json配置文件中,将H5平台的origin字段配置为后端服务器的域名,以防止跨域问题的出现。 总结起来,Uniapp可以通过选择图片视频文件,再通过uni.uploadFile方法将文件上传到后端服务器,实现H5手机上传图片视频的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值