汽车公司可能用到的知识点

iOS 上传本地视频或者图片(转载)

2020-05-11  本文已影响0人  辛乐

转载:https://www.cnblogs.com/zhouxihi/p/6526304.html

以下纯转载上述地址老铁的文章,只为本人记载后续使用,详情参考上边连接!!!

iOS实现视频和图片的上传

关于iOS如何实现视频和图片的上传, 我们先理清下思路

思路:

1. 如何获取图片?

2. 如何获取视频?

3. 如何把图片存到缓存路径中?

4. 如何把视频存到缓存路径中?

5. 如何上传?

接下来, 我们按照上面的思路一步一步实现

首先我们新建一个类, 用来储存每一个要上传的文件uploadModel.h

复制代码

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">#import <Foundation/Foundation.h>

@interface uploadModel : NSObject

@property (nonatomic, strong) NSString *path;
@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) BOOL isUploaded; @end</pre>

复制代码

1. 如何获取图片?

从相册选择 或者 拍照,

这部分可以用UIImagePickerController来实现

代码如下:

复制代码

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">- (void)actionPhoto {

UIAlertController *alertController  = \
[UIAlertController alertControllerWithTitle:@"" message:@"上传照片" preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *photoAction  = \
[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * _Nonnull action) {

                           NSLog(@"从相册选择");
                           self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                           self.imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
                           self.imagePicker.allowsEditing = YES;

                           [self presentViewController:self.imagePicker
                                              animated:YES
                                            completion:nil];

                       }];

UIAlertAction *cameraAction = \
[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * _Nonnull action) {

                           NSLog(@"拍照"); if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

                               self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                               self.imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
                               self.imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
                               self.imagePicker.allowsEditing = YES;

                               [self presentViewController:self.imagePicker
                                                  animated:YES
                                                completion:nil];
                           }
                       }];

UIAlertAction *cancelAction = \
[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                       handler:^(UIAlertAction * _Nonnull action) {

                           NSLog(@"取消");
                       }];

[alertController addAction:photoAction];
[alertController addAction:cameraAction];
[alertController addAction:cancelAction];

[self presentViewController:alertController animated:YES completion:nil];

}</pre>

复制代码

2. 如果获取视频?

从相册选择 或者 拍摄

这部分也可以用UIImagePickerController来实现

代码:

复制代码

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">- (void)actionVideo {

UIAlertController *alertController = \
[UIAlertController alertControllerWithTitle:@"" message:@"上传视频" preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *photoAction = \
[UIAlertAction actionWithTitle:@"从视频库选择" style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * _Nonnull action) {

                           NSLog(@"从视频库选择");
                           self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                           self.imagePicker.mediaTypes = @[(NSString *)kUTTypeMovie];
                           self.imagePicker.allowsEditing = NO;

                           [self presentViewController:self.imagePicker animated:YES completion:nil];
                       }];

UIAlertAction *cameraAction = \
[UIAlertAction actionWithTitle:@"录像" style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * _Nonnull action) {

                           NSLog(@"录像");
                           self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                           self.imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
                           self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
                           self.imagePicker.videoQuality = UIImagePickerControllerQualityType640x480;
                           self.imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
                           self.imagePicker.allowsEditing = YES;

                           [self presentViewController:self.imagePicker animated:YES completion:nil];
                       }];

UIAlertAction *cancelAction = \
[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                       handler:^(UIAlertAction * _Nonnull action) {

                           NSLog(@"取消");
                       }];

[alertController addAction:photoAction];
[alertController addAction:cameraAction];
[alertController addAction:cancelAction];

[self presentViewController:alertController animated:YES completion:nil];

}</pre>

复制代码

3, 关于缓存, 如何把照片存入缓存目录?

这部分我们先考虑缓存目录, 一般存在Document 或者 Temp里面

我们给图片和视频各创建一个缓存目录:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">#define PHOTOCACHEPATH [NSTemporaryDirectory() stringByAppendingPathComponent:@"photoCache"]

define VIDEOCACHEPATH [NSTemporaryDirectory() stringByAppendingPathComponent:@"videoCache"]</pre>

把UIImage存入缓存的方法:

复制代码

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">//将Image保存到缓存路径中

复制代码

4. 如何把视频存入缓存?

把视频存入缓存的方法:

复制代码

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">//将视频保存到缓存路径中

复制代码

从缓存获取图片的方法:

复制代码

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">//从缓存路径获取照片

复制代码

上传图片和视频的时候我们一般会利用当前时间给文件命名, 方法如下

复制代码

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">//以当前时间合成图片名称

复制代码

有时候需要获取视频的第一帧作为显示, 方法如下:

复制代码

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">//获取视频的第一帧截图, 返回UIImage //需要导入AVFoundation.h

复制代码

5. 如何上传?

下面就是上传方法:

我把服务器地址xx掉了, 大家可以改为自己的

复制代码

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">//上传图片和视频

复制代码

这里有事先创建两个可变数组uploadArray, uploadedArray, 一个存放准要上传的内容, 一个存放上传完的内容

在准备上传后做什么操作, 可以检查两个数组的数量是否相等

最后是UIImagePickerController的协议方法

复制代码

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">#pragma mark - UIImagePickerDelegate methods

[self.uploadArray addObject:model];
} //[picker dismissViewControllerAnimated:YES completion:nil];
} - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:nil];

}</pre>

上一篇下一篇

猜你喜欢

热点阅读