UploadVideoViewController.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // UploadVideoViewController.m
  3. // model
  4. //
  5. // Created by Drew on 2019/1/3.
  6. // Copyright © 2019 Mine. All rights reserved.
  7. //
  8. #import <MediaPlayer/MediaPlayer.h>
  9. #import "UploadVideoViewController.h"
  10. #import "TOCropViewController.h"
  11. #import <Photos/Photos.h>
  12. #import "AFNetworking.h"
  13. @interface UploadVideoViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, TOCropViewControllerDelegate>
  14. @property(weak, nonatomic) IBOutlet UITextField *descText;
  15. @property(weak, nonatomic) IBOutlet UIButton *playBtn;
  16. @property(weak, nonatomic) IBOutlet UIButton *coverBtn;
  17. @property(weak, nonatomic) IBOutlet UIImageView *coverImg;
  18. @property(weak, nonatomic) IBOutlet UIView *videoView;
  19. @property(nonatomic, strong) NSURL *videoURL;
  20. @property(nonatomic, strong) NSString *isSgined;
  21. @end
  22. @implementation UploadVideoViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. self.navigationItem.title = @"活动报名";
  26. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
  27. self.navigationItem.leftBarButtonItem = leftItem;
  28. self.navigationController.navigationBar.tintColor = [UIColor blackColor];
  29. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  30. switch (status) {
  31. case PHAuthorizationStatusAuthorized:
  32. NSLog(@"PHAuthorizationStatusAuthorized");
  33. break;
  34. case PHAuthorizationStatusDenied:
  35. NSLog(@"PHAuthorizationStatusDenied");
  36. break;
  37. case PHAuthorizationStatusNotDetermined:
  38. NSLog(@"PHAuthorizationStatusNotDetermined");
  39. break;
  40. case PHAuthorizationStatusRestricted:
  41. NSLog(@"PHAuthorizationStatusRestricted");
  42. break;
  43. }
  44. }];
  45. self.isSgined = @"0";
  46. NSString *url = [NSString stringWithFormat:@"%@/activity?action=getactivity&pk=%@&memberpk=%@", PublicUrl, self.activitypk, [Helper sharedAccount].accid];
  47. [[AHHttpManager sharedManager]
  48. POST:url
  49. parameters:nil
  50. success:^(id responseObject) {
  51. if ([@"success" isEqualToString:responseObject[@"msg"]]) {
  52. NSDictionary *data = responseObject[@"data"];
  53. self.isSgined = [data[@"signed"] stringValue];
  54. ActivityModel *model = [[ActivityModel alloc] initWithDictionary:data error:nil];
  55. self.model = model;
  56. }
  57. }
  58. failure:^(NSError *error) {
  59. }];
  60. }
  61. - (void)backClick {
  62. [self.navigationController popViewControllerAnimated:YES];
  63. }
  64. - (void)viewWillAppear:(BOOL)animated {
  65. [super viewWillAppear:animated];
  66. self.navigationController.navigationBar.tintColor = [UIColor blackColor];
  67. [self.navigationController.navigationBar setShadowImage:[UIImage new]];
  68. [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  69. }
  70. - (void)viewWillDisappear:(BOOL)animated {
  71. [super viewWillDisappear:animated];
  72. [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
  73. [self.navigationController.navigationBar setShadowImage:nil];
  74. }
  75. - (IBAction)chooseVideo:(id)sender {
  76. UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
  77. pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  78. pickerController.mediaTypes = @[(NSString *) kUTTypeMovie];
  79. pickerController.allowsEditing = YES;
  80. pickerController.videoQuality = UIImagePickerControllerQualityTypeMedium;//视频质量
  81. pickerController.videoMaximumDuration = 30.f;//视频最长长度
  82. pickerController.delegate = self;
  83. [self.navigationController presentViewController:pickerController animated:YES completion:nil];
  84. }
  85. - (IBAction)chooseCover:(id)sender {
  86. UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
  87. pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;//图片分组列表样式
  88. pickerController.delegate = self;
  89. [self.navigationController presentViewController:pickerController animated:YES completion:nil];
  90. }
  91. - (IBAction)play:(id)sender {
  92. if (self.videoURL) {
  93. MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:self.videoURL];
  94. playerViewController.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
  95. [self presentMoviePlayerViewControllerAnimated:playerViewController];
  96. }
  97. }
  98. - (IBAction)confirm:(id)sender {
  99. if (!self.model) {
  100. return;
  101. }
  102. if ([self.isSgined intValue] > 0) {
  103. [MBProgressHUD showInfo:@"请勿重复报名"];
  104. return;
  105. }
  106. if (!self.videoURL) {
  107. [MBProgressHUD showInfo:@"请选择视频"];
  108. } else if (!self.descText.text.length) {
  109. [MBProgressHUD showInfo:@"请填写描述"];
  110. } else {
  111. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  112. hud.label.text = @"正在上传";
  113. hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
  114. hud.removeFromSuperViewOnHide = YES;
  115. NSString *parttypk = self.model.pk;
  116. NSString *pk = [ModelUser user].pk;
  117. NSString *url = [NSString stringWithFormat:@"%@/activity?action=doEnroll&PK=%@&ParttyPK=%@&people=%@&Tel=%@&pCount=%@&Fee=%@&desc=%@", PublicUrl, pk, parttypk, [ModelUser user].pet, @"", @"1", @"0", self.descText.text];
  118. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  119. manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  120. manager.requestSerializer = [AFJSONRequestSerializer serializer];
  121. manager.securityPolicy.allowInvalidCertificates = YES;
  122. [manager POST:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:@{} constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
  123. NSData *imgData = UIImageJPEGRepresentation(self.coverImg.image, 0.5);
  124. [formData appendPartWithFormData:imgData name:@"image"];
  125. [formData appendPartWithFileData:imgData name:@"image" fileName:@"image.jpg" mimeType:@"image/jpeg"];
  126. NSData *videoData = [NSData dataWithContentsOfURL:self.videoURL];
  127. [formData appendPartWithFileData:videoData name:@"video" fileName:@"video.mov" mimeType:@"video/quicktime"];
  128. } progress:^(NSProgress *uploadProgress) {
  129. NSLog(@"progress %lli / %lli", uploadProgress.completedUnitCount, uploadProgress.totalUnitCount);
  130. hud.progress = uploadProgress.completedUnitCount / (float) uploadProgress.totalUnitCount;
  131. } success:^(NSURLSessionDataTask *task, id responseObject) {
  132. [hud hideAnimated:YES];
  133. [MBProgressHUD showInfo:@"报名成功"];
  134. [self.navigationController popToRootViewControllerAnimated:YES];
  135. } failure:^(NSURLSessionDataTask *task, NSError *error) {
  136. [hud hideAnimated:YES];
  137. NSLog(@"error:%@", error);
  138. [MBProgressHUD showInfo:@"上传失败,请稍后再试"];
  139. }];
  140. }
  141. }
  142. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {
  143. if (picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum) {
  144. UIImage *image = info[UIImagePickerControllerOriginalImage];
  145. TOCropViewController *cropViewController = [[TOCropViewController alloc] initWithImage:image];
  146. cropViewController.delegate = self;
  147. cropViewController.customAspectRatio = CGSizeMake(300, 300);
  148. cropViewController.aspectRatioLockEnabled = YES;
  149. cropViewController.resetAspectRatioEnabled = NO;
  150. [picker dismissViewControllerAnimated:YES completion:nil];
  151. [self presentViewController:cropViewController animated:YES completion:nil];
  152. } else {
  153. NSURL *url = info[UIImagePickerControllerMediaURL];//获得视频的URL
  154. NSLog(@"url %@", url);
  155. self.videoURL = url;
  156. // MPMoviePlayerController *playerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
  157. // playerController.view.frame = CGRectMake(0, 0, self.videoView.frame.size.width, self.videoView.frame.size.height);
  158. // [self.videoView addSubview:playerController.view];//第四步:设置播放器属性
  159. // playerController.controlStyle = MPMovieControlStyleDefault;
  160. // playerController.shouldAutoplay = NO;
  161. // playerController.scalingMode = MPMovieScalingModeAspectFit;
  162. // playerController.repeatMode = MPMovieRepeatModeOne;
  163. // [playerController prepareToPlay];
  164. // [playerController play];
  165. [picker dismissViewControllerAnimated:YES completion:nil];
  166. AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
  167. CMTime audioDuration = asset.duration;
  168. NSNumber *duration = @([@(CMTimeGetSeconds(audioDuration)) intValue]);
  169. AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
  170. gen.appliesPreferredTrackTransform = YES;
  171. CMTime time = CMTimeMakeWithSeconds(0.0, 600);
  172. NSError *error = nil;
  173. CMTime actualTime;
  174. CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
  175. UIImage *thumbnail = [[UIImage alloc] initWithCGImage:image];
  176. CGImageRelease(image);
  177. self.coverImg.image = thumbnail;
  178. }
  179. }
  180. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  181. [picker dismissViewControllerAnimated:YES completion:nil];
  182. }
  183. - (void)cropViewController:(TOCropViewController *)cropViewController didCropToImage:(UIImage *)image withRect:(CGRect)cropRect angle:(NSInteger)angle {
  184. self.coverImg.image = image;
  185. [cropViewController dismissViewControllerAnimated:YES completion:nil];
  186. }
  187. @end