// // 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 #define PHOTOCACHEPATH [NSTemporaryDirectory() stringByAppendingPathComponent:@"photoCache"] #define VIDEOCACHEPATH [NSTemporaryDirectory() stringByAppendingPathComponent:@"videoCache"] @interface SubmitInformation_VC () { 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 *)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{ if (textTF.text.length==0) { ALERTSHOW(@"请添加描述"); }else{ [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 *)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