| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543 |
- //
- // SubmitInformation_VC.m
- // Haishenghai-master
- //
- // Created by GG on 2019/1/7.
- // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved.
- //
- #import "SubmitInformation_VC.h"
- #import "Header.h"
- #import <AVFoundation/AVFoundation.h>
- #define PHOTOCACHEPATH [NSTemporaryDirectory() stringByAppendingPathComponent:@"photoCache"]
- #define VIDEOCACHEPATH [NSTemporaryDirectory() stringByAppendingPathComponent:@"videoCache"]
- @interface SubmitInformation_VC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
- {
- UIButton *imageButton;
- UIButton *videoButton;
- UITextField *textTF;
- }
- @end
- @implementation SubmitInformation_VC
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor =[UIColor whiteColor];
- self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:60/255.0 green:114/255.0 blue:255/255.0 alpha:1];
- [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
- UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [backBtn setImage:[UIImage imageNamed:@"hsh_return"] forState:UIControlStateNormal];
- backBtn.frame = CGRectMake(0, 0, 44, 44);
- [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
- backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
- self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
-
- [self setupUI];
-
- }
- -(void)backClick{
- [self.navigationController popViewControllerAnimated:YES];
- }
- -(void)setupUI{
- _myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64-44) style:UITableViewStylePlain];
- _myTable.dataSource = self;
- _myTable.delegate = self;
- _myTable.showsVerticalScrollIndicator = NO;
- _myTable.separatorColor = [UIColor whiteColor];
- _myTable.scrollEnabled = NO;
- [self.view addSubview:_myTable];
-
- UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- commitBtn.frame = CGRectMake(0, HEIGHT-44, WIDTH, 44);
- // [commitBtn setBackgroundImage:[UIImage imageNamed:@"blue"] forState:UIControlStateNormal];
- commitBtn.backgroundColor = [UIColor colorWithRed:78/255.0 green:105/255.0 blue:255/255.0 alpha:1];
- [commitBtn setTitle:@"提交" forState:UIControlStateNormal];
- [commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
-
- commitBtn.titleLabel.font = [UIFont systemFontOfSize:18];
- commitBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
- [commitBtn addTarget:self action:@selector(commitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:commitBtn];
-
- _imagePicker = [[UIImagePickerController alloc]init];
- _imagePicker.delegate =self;
- }
- #pragma mark-----tableView Delegae
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
-
- return 1;
- }
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- return 4;
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
- if (section==3) {
- return 0;
- }else{
- return 1;
- }
- return 0;
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- if (indexPath.section ==0) {
- return 90;
- }else if (indexPath.section ==1){
- return HEIGHT/4-20;
- }else if (indexPath.section==2){
- return HEIGHT/4;
- }else{
- return HEIGHT/4-20;
- }
- return 0;
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
- return 0;
- }
- -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
- UIView *footerView = [[UIView alloc]init];
- footerView.backgroundColor = [UIColor colorWithRed:227/255.0 green:227/255.0 blue:227/255.0 alpha:1];
- footerView.frame = CGRectMake(0, 0, WIDTH, 1);
- return footerView;
- }
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- static NSString *cellId = @"cell ";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
- if (!cell) {
- cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
- }
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- if (indexPath.section==0) {
- UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 20)];
- titleLabel.text =@"警情描述:";
- titleLabel.font =[UIFont systemFontOfSize:18];
- [cell.contentView addSubview:titleLabel];
-
- textTF = [[UITextField alloc]initWithFrame:CGRectMake(10, 35, WIDTH-20, 44)];
- textTF.delegate = self;
- textTF.placeholder = @"请添加警情文字描述";
- textTF.backgroundColor = [UIColor colorWithRed:227/255.0 green:227/255.0 blue:227/255.0 alpha:1];
- textTF.layer.cornerRadius = 10;
- [cell.contentView addSubview:textTF];
- }else if (indexPath.section==1){
- UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 25)];
- titleLabel.text =@"图片:";
- titleLabel.font =[UIFont systemFontOfSize:18];
- [cell.contentView addSubview:titleLabel];
-
- // for (int i =0; i<3; i++) {
- imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
- imageButton.frame = CGRectMake(25, 35, (WIDTH-40)/3-30, (WIDTH-40)/3-30);
- // imageButton.tag = i+1;
- // if (imageButton.tag==1) {
- [imageButton setImage:[UIImage imageNamed:@"hsh_news_add_image"] forState:UIControlStateNormal];
- // }
- [imageButton addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
- [cell.contentView addSubview:imageButton];
- // }
- }else if (indexPath.section==2){
- UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 25)];
- titleLabel.text =@"视频:";
- titleLabel.font =[UIFont systemFontOfSize:18];
- [cell.contentView addSubview:titleLabel];
-
- videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
- videoButton.frame = CGRectMake(40,35,WIDTH-80,HEIGHT/4-38);
- [videoButton setImage:[UIImage imageNamed:@"hsh_news_video"] forState:UIControlStateNormal];
- [videoButton addTarget:self action:@selector(videoButtonClick:) forControlEvents:UIControlEventTouchUpInside];
- [cell.contentView addSubview:videoButton];
- }else{
- UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 25)];
- titleLabel.text =@"音频:";
- titleLabel.font =[UIFont systemFontOfSize:18];
- [cell.contentView addSubview:titleLabel];
-
- UIButton *audioButton = [UIButton buttonWithType:UIButtonTypeCustom];
- audioButton.frame = CGRectMake(10,35,WIDTH-20,44);
- audioButton.layer.cornerRadius = 10;
- [audioButton setTitle:@"按住录音" forState:UIControlStateNormal];
- [audioButton addTarget:self action:@selector(audioButtonClick:) forControlEvents:UIControlEventTouchUpInside];
- audioButton.backgroundColor = [UIColor colorWithRed:227/255.0 green:227/255.0 blue:227/255.0 alpha:1];
- [cell.contentView addSubview:audioButton];
- }
- return cell;
- }
- #pragma mark----Event----
- -(void)addBtnClick:(UIButton *)addBtn{
- [self actionPhoto];
- }
- -(void)videoButtonClick:(UIButton *)videoBtn{
- [self actionVideo];
- }
- -(void)audioButtonClick:(UIButton *)videoBtn{
-
- }
- //添加图片
- -(void)actionPhoto{
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
- message:nil
- 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];
- }
- //录取视频
- -(void)actionVideo{
- UIAlertController *alertController =[UIAlertController alertControllerWithTitle:nil
- message:nil
- 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];
- }
- //将Image保存到缓存路径中
- - (void)saveImage:(UIImage *)image toCachePath:(NSString *)path {
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if (![fileManager fileExistsAtPath:PHOTOCACHEPATH]) {
-
- NSLog(@"路径不存在, 创建路径");
- [fileManager createDirectoryAtPath:PHOTOCACHEPATH
- withIntermediateDirectories:YES
- attributes:nil
- error:nil];
- } else {
-
- NSLog(@"路径存在");
- }
-
- //[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
- [UIImageJPEGRepresentation(image, 1) writeToFile:path atomically:YES];
- }
- //将视频保存到缓存路径中
- - (void)saveVideoFromPath:(NSString *)videoPath toCachePath:(NSString *)path {
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if (![fileManager fileExistsAtPath:VIDEOCACHEPATH]) {
-
- NSLog(@"路径不存在, 创建路径");
- [fileManager createDirectoryAtPath:VIDEOCACHEPATH
- withIntermediateDirectories:YES
- attributes:nil
- error:nil];
- } else {
-
- NSLog(@"路径存在");
- }
-
- NSError *error;
- [fileManager copyItemAtPath:videoPath toPath:path error:&error];
- if (error) {
-
- NSLog(@"文件保存到缓存失败");
- }
- }
- //从缓存路径获取照片
- - (UIImage *)getImageFromPath:(NSString *)path {
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if ([fileManager fileExistsAtPath:path]) {
-
- return [UIImage imageWithContentsOfFile:path];
- }
-
- return nil;
- }
- //以当前时间合成图片名称
- - (NSString *)getImageNameBaseCurrentTime {
-
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"yyyy-MM-dd HH-mm-ss"];
-
- return [[dateFormatter stringFromDate:[NSDate date]] stringByAppendingString:@".JPG"];
- }
- //以当前时间合成视频名称
- - (NSString *)getVideoNameBaseCurrentTime {
-
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"yyyy-MM-dd HH-mm-ss"];
-
- return [[dateFormatter stringFromDate:[NSDate date]] stringByAppendingString:@".mp3"];
- }
- //获取视频的第一帧截图, 返回UIImage
- //需要导入AVFoundation.h
- - (UIImage*) getVideoPreViewImageWithPath:(NSURL *)videoPath
- {
- AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoPath options:nil];
-
- AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
- gen.appliesPreferredTrackTransform = YES;
-
- CMTime time = CMTimeMakeWithSeconds(0.0, 600);
- NSError *error = nil;
-
- CMTime actualTime;
- CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
- UIImage *img = [[UIImage alloc] initWithCGImage:image];
-
- return img;
- }
- //上传图片和视频
- - (void)uploadImageAndMovieBaseModel:(uploadModel *)model {
-
- //获取文件的后缀名
- NSString *extension = [model.name componentsSeparatedByString:@"."].lastObject;
-
- //设置mimeType
- NSString *mimeType;
- if ([model.type isEqualToString:@"image"]) {
-
- mimeType = [NSString stringWithFormat:@"image/%@", extension];
- } else {
-
- mimeType = [NSString stringWithFormat:@"video/%@", extension];
- }
-
-
- }
- #pragma mark - UIImagePickerDelegate methods
- - (void)imagePickerController:(UIImagePickerController *)picker
- didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
-
- [picker dismissViewControllerAnimated:YES completion:nil];
-
- //获取用户选择或拍摄的是照片还是视频
- NSString *mediaType = info[UIImagePickerControllerMediaType];
-
- if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
-
- //获取编辑后的照片
- NSLog(@"获取编辑后的好片");
- UIImage *tempImage = info[UIImagePickerControllerEditedImage];
- [imageButton setImage:tempImage forState:UIControlStateNormal];
-
- //在这里写图片上传的方法
- // [self uploadPhoto];
- //将照片存入相册
- if (tempImage) {
-
- if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
-
- //将照片存入相册
- NSLog(@"将照片存入相册");
- UIImageWriteToSavedPhotosAlbum(tempImage, self, nil, nil);
- }
- //获取图片名称
- NSLog(@"获取图片名称");
- NSString *imageName = [self getImageNameBaseCurrentTime];
- NSLog(@"图片名称: %@", imageName);
-
- //将图片存入缓存
- NSLog(@"将图片写入缓存");
- [self saveImage:tempImage
- toCachePath:[PHOTOCACHEPATH stringByAppendingPathComponent:imageName]];
-
- //创建uploadModel
- NSLog(@"创建model");
- uploadModel *model = [[uploadModel alloc] init];
-
- model.path = [PHOTOCACHEPATH stringByAppendingPathComponent:imageName];
- model.name = imageName;
- model.type = @"image";
- model.isUploaded = NO;
-
- //将模型存入待上传数组
- NSLog(@"将Model存入待上传数组");
- [self.uploadArray addObject:model];
- }
- }
-
- else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
-
- if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
-
- //如果是拍摄的视频, 则把视频保存在系统多媒体库中
- NSLog(@"video path: %@", info[UIImagePickerControllerMediaURL]);
- [videoButton setImage:[self getVideoPreViewImageWithPath:info[UIImagePickerControllerMediaURL]] forState:UIControlStateNormal];
-
- // ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
- // [library writeVideoAtPathToSavedPhotosAlbum:info[UIImagePickerControllerMediaURL] completionBlock:^(NSURL *assetURL, NSError *error) {
- //
- // if (!error) {
- //
- // NSLog(@"视频保存成功");
- // } else {
- //
- // NSLog(@"视频保存失败");
- // }
- // }];
- }
-
- //生成视频名称
- NSString *mediaName = [self getVideoNameBaseCurrentTime];
- NSLog(@"mediaName: %@", mediaName);
- //在这里写视频上传的方法
- //将视频存入缓存
- NSLog(@"将视频存入缓存");
- [self saveVideoFromPath:info[UIImagePickerControllerMediaURL] toCachePath:[VIDEOCACHEPATH stringByAppendingPathComponent:mediaName]];
-
- //创建uploadmodel
- uploadModel *model = [[uploadModel alloc] init];
-
- model.path = [VIDEOCACHEPATH stringByAppendingPathComponent:mediaName];
- model.name = mediaName;
- model.type = @"moive";
- model.isUploaded = NO;
-
- //将model存入待上传数组
- [self.uploadArray addObject:model];
- }
-
- //[picker dismissViewControllerAnimated:YES completion:nil];
-
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
-
-
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
- #pragma mark-----提交的请求
- -(void)commitBtnClick:(UIButton *)btn{
-
- NSString *inputText = [textTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- if (inputText.length ==0) {
- ALERTSHOW(@"输入不能为空");
- return;
- }
- [DataSourceManager processDeviceTroubleWithAudioIDS:@"" Explain:textTF.text movieIDS:@"" pictureIDS:@"" ProType:@"1" ProcessId:_processID completionBlock:^(NSDictionary *dic) {
- UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
- UIViewController *topVC = appRootVC;
- if (topVC.presentedViewController) {
- topVC = topVC.presentedViewController;
- }
-
- MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:topVC.view animated:YES];
- hud.mode = MBProgressHUDModeText;
- hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
-
- hud.bezelView.color = [UIColor grayColor];
- hud.label.text = @"提交成功";
- hud.label.textColor = [UIColor whiteColor];
- hud.animationType = MBProgressHUDAnimationFade; //默认类型的,渐变
- [hud hideAnimated:YES afterDelay:1];
- [self.navigationController popViewControllerAnimated:YES];
- }];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark UITextField代理方法
- -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
-
- [self.view endEditing:YES];
- }
- - (BOOL)textFieldShouldReturn:(UITextField *)textField {
- [textField resignFirstResponder];
- return YES;
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|