YOUPAIZYSendVideoByLinkVC.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // YOUPAIZYSendVideoByLinkVC.m
  3. // VQU
  4. //
  5. // Created by Elaine on 2020/11/9.
  6. // Copyright © 2020 leo. All rights reserved.
  7. //
  8. #import "YOUPAIZYSendVideoByLinkVC.h"
  9. #import "UIViewController+TFPresent.h"
  10. #import "LZAlertWindow.h"
  11. #import "YOUPAILPChatTipView.h"
  12. #import "YOUPAIZYSendVideoByLocalVC.h"
  13. #import "YOUPAISHMediaManager.h"//直接裁剪类
  14. @interface YOUPAIZYSendVideoByLinkVC ()<NSURLSessionDelegate>
  15. @property (nonatomic,strong)UITextField* youpaiptextField;
  16. @property(nonatomic,strong)UIButton *youpaipsendVideoBtn;//发布视频
  17. @property(nonatomic,strong)NSURLSessionDownloadTask *youpaipdownloadTask;
  18. @end
  19. @implementation YOUPAIZYSendVideoByLinkVC
  20. -(void)viewWillAppear:(BOOL)animated{
  21. [super viewWillAppear:animated];
  22. // [self youpaifaddTip];
  23. }
  24. - (void)youpaifaddTip{
  25. NSString *tip = @"请勿涉及色情,广告等违规内容,违者将永久封号";
  26. YOUPAILPChatTipView* chatTip = [[YOUPAILPChatTipView alloc]initWithFrame:CGRectMake(0,NavBarHeight-40, KScreenWidth, 40)];
  27. chatTip.tipLabel.text = tip;
  28. [self.view addSubview:chatTip];
  29. [UIView animateWithDuration:1 animations:^{
  30. chatTip.frame = CGRectMake(0,NavBarHeight, KScreenWidth, 40);
  31. }completion:^(BOOL finished) {
  32. if (finished) {
  33. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  34. [UIView animateWithDuration:1 animations:^{
  35. chatTip.frame = CGRectMake(0,NavBarHeight-40, KScreenWidth, 40);
  36. }completion:^(BOOL finished) {
  37. if (finished) {
  38. [chatTip removeFromSuperview];
  39. }
  40. }];
  41. });
  42. }
  43. }];
  44. }
  45. - (void)viewDidLoad {
  46. [super viewDidLoad];
  47. self.title = @"链接上传";
  48. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(youpaifhideTheKeyboard)];
  49. [self.view addGestureRecognizer:tap];
  50. // self.view.backgroundColor = HexColorFromRGB(0xFFFFFF);
  51. self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifcancelClick) image:[UIImage imageNamed:@"vqu_images_navigation_back_black"]];
  52. UIButton *rightBgV = [UIButton buttonWithType:UIButtonTypeCustom];
  53. [rightBgV addTarget:self action:@selector(youpaifrightClick) forControlEvents:UIControlEventTouchUpInside];
  54. UIImageView *rightImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vqu_images_D_senddynamic_right"]];
  55. rightImgV.frame = CGRectMake(44.0f - 16.0f, (44.0f - 16.0f) / 2.0f, 16.0f, 16.0f);
  56. [rightBgV addSubview:rightImgV];
  57. UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBgV];
  58. self.navigationItem.rightBarButtonItem = rightBarItem;//[UIBarButtonItem itemWithTarget:self action:@selector(youpaifrightClick) image:[UIImage imageNamed:@"ic_senddynamic_right"]];
  59. // ZCLiveTextView* liveView = [[ZCLiveTextView alloc]initWithFrame:CGRectMake(15, 15+NavBarHeight, KScreenWidth-30, 100) TopMargin:8 LeftMargin:8];
  60. // liveView.textLength= 140;
  61. // liveView.backgroundColor = [UIColor whiteColor];
  62. // liveView.placeholder = @"这一刻你想说点什么...";
  63. // liveView.placeholderColor = HexColorFromRGB(0xb5b5b5);
  64. // liveView.timeoutAlertColor = LCRedColor;
  65. // liveView.layer.cornerRadius = 10.0;
  66. // self.liveView = liveView;
  67. // [self.view addSubview:liveView];
  68. // UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(liveView.frame)+16,KScreenWidth - 32,0.5)];
  69. // line.backgroundColor = HexColorFromRGB(0xEEEEEE);
  70. // [self.view addSubview:line];
  71. UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(14.0f,NavBarHeight + 24.0f, KScreenWidth-28.0f, 48.0f)];
  72. textField.layer.cornerRadius = 5.0f;
  73. textField.layer.masksToBounds = YES;
  74. textField.clearButtonMode = UITextFieldViewModeWhileEditing;
  75. [[textField valueForKey:@"_clearButton"] setImage:[UIImage imageNamed:@"vqu_images_D_nick_clean"] forState:UIControlStateNormal];
  76. textField.font = LCFont(14);
  77. textField.textColor = [UIColor whiteColor];
  78. textField.tintColor = ZYGradientTwoColor;
  79. self.youpaiptextField = textField;
  80. textField.backgroundColor = HexColorFromRGB(0x2A2935);
  81. UIView *leftPwdView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,12.0f, 48)];
  82. textField.leftView = leftPwdView;
  83. textField.leftViewMode = UITextFieldViewModeAlways;
  84. NSString *holderText = @"粘贴抖音或其他平台分享来的视频链接";
  85. NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
  86. [placeholder addAttribute:NSForegroundColorAttributeName value:HexColorFromRGB(0x6C6B70) range:NSMakeRange(0, holderText.length)];
  87. [placeholder addAttribute:NSFontAttributeName value:LCFont(14) range:NSMakeRange(0, holderText.length)];
  88. textField.attributedPlaceholder = placeholder;
  89. [self.view addSubview:textField];
  90. self.youpaipsendVideoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  91. self.youpaipsendVideoBtn.frame = CGRectMake(40.0f,KScreenHeight-SafeHeight-77.0f,KScreenWidth - 80.0f,48);
  92. [self.youpaipsendVideoBtn setTitle:@"下一步" forState:0];
  93. [self.youpaipsendVideoBtn setTitleColor:[UIColor whiteColor] forState:0];
  94. self.youpaipsendVideoBtn.titleLabel.font = LCFont(16);
  95. self.youpaipsendVideoBtn.layer.cornerRadius = 24;
  96. self.youpaipsendVideoBtn.layer.masksToBounds = YES;
  97. [self.youpaipsendVideoBtn addTarget:self action:@selector(youpaifsendClick) forControlEvents:UIControlEventTouchUpInside];
  98. self.youpaipsendVideoBtn.backgroundColor = HexColorFromRGB(0x4F4B5B);
  99. self.youpaipsendVideoBtn.userInteractionEnabled = NO;
  100. [self.view addSubview:self.youpaipsendVideoBtn];
  101. }
  102. - (void)youpaifcancelClick{
  103. [self popEffectDismiss];
  104. // // self.sendModel.tit = self.liveView.textView.text;
  105. // UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"要保存草稿吗?" preferredStyle:UIAlertControllerStyleAlert];
  106. // UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"放弃" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  107. // // [LCSaveModel saveSendDynamicModel:nil];
  108. // [self popEffectDismiss];
  109. // }];
  110. // UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  111. // // [LCSaveModel saveSendDynamicModel:self.sendModel];
  112. // [self popEffectDismiss];
  113. // }];
  114. // [alertController addAction:cancelAction];
  115. // [alertController addAction:okAction];
  116. // [self presentViewController:alertController animated:YES completion:nil];
  117. }
  118. //提示
  119. -(void)youpaifrightClick{
  120. LZAlertAction *action = [LZAlertAction actionWithTitle:@"我知道了" handler:^(LZAlertAction *action) {
  121. }];
  122. action.bgColor = [UIColor colorWithPatternImage:[LCTools ColorImage:CGSizeMake(KScreenWidth - 128.0f, 48.0f) FromColors:@[LZBFB6FFColor,LZ7C69FEColor] ByGradientType:GradientLeftToRight]];
  123. LZAlertWindow *alert = [LZAlertWindow alertWithTitle:@"发布须知" content:@"1、禁止发布色情,性暗示等低俗内容;\n\n2、禁止发布国家政治,暴恐暴乱等内容;\n\n3、不可涉及第三方平台信息;\n\n4、不可使用他人的图片或视频发布。" action:@[action]];
  124. alert.contentFont = LCFont16;
  125. alert.contentHorizontalSpace = 32.0f;
  126. [self TFPresentVC:alert completion:^{}];
  127. }
  128. //发布视频----下载视频到本地
  129. -(void)youpaifsendClick
  130. {
  131. if (self.youpaiptextField.text.length == 0) {
  132. [ZCHUDHelper showTitle:@"链接不能为空"];
  133. return;
  134. }
  135. NSLog(@"%@",self.youpaiptextField.text);
  136. [LCHttpHelper requestWithURLString:Watermark parameters:@{@"shear_plate":self.youpaiptextField.text} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  137. NSDictionary* dict = (NSDictionary*)responseObject;
  138. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  139. if (code==0) {//成功
  140. NSString* video_url = [[dict objectForKey:@"data"] objectForKey:@"video_url"];
  141. // NSString* imageUrl = [[dict objectForKey:@"data"] objectForKey:@"image_server"];
  142. NSLog(@"video_url = %@",video_url);
  143. [self youpaifstartDownLoadVedioWithUrl:video_url];
  144. }
  145. } failure:^(NSError *error) {
  146. }];
  147. }
  148. /** 下载视频 */
  149. - (void)youpaifstartDownLoadVedioWithUrl:(NSString *)url {
  150. NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
  151. NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
  152. self.youpaipdownloadTask = [session downloadTaskWithURL:[NSURL URLWithString:url]];
  153. [self.youpaipdownloadTask resume];
  154. [ZCHUDHelper showWithStatus:@"视频下载中..."];
  155. }
  156. #pragma mark NSSessionUrlDelegate
  157. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
  158. didWriteData:(int64_t)bytesWritten
  159. totalBytesWritten:(int64_t)totalBytesWritten
  160. totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
  161. {
  162. //下载进度
  163. CGFloat progress = totalBytesWritten / (double)totalBytesExpectedToWrite;
  164. dispatch_async(dispatch_get_main_queue(), ^{
  165. //进行UI操作 设置进度条
  166. // self.progressView.progressValue = progress;
  167. // self.progressView.contentLabel.text = [NSString stringWithFormat:@"%.2f%%",progress*100];
  168. });
  169. }
  170. //下载完成 保存到本地相册
  171. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
  172. didFinishDownloadingToURL:(NSURL *)location
  173. {
  174. [ZCHUDHelper showTitle:@"下载完成"];
  175. //1.拿到cache文件夹的路径
  176. NSString *cache=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
  177. //2,拿到cache文件夹和文件名
  178. NSString *file=[cache stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
  179. [[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:file] error:nil];
  180. NSLog(@"file = %@",file);
  181. //3,保存视频到相册
  182. if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(file)) {
  183. //保存相册核心代码
  184. UISaveVideoAtPathToSavedPhotosAlbum(file,self,@selector(youpaifsavedVideoPhotoImage:didFinishSavingWithError:contextInfo:),nil);
  185. }
  186. AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:file]];
  187. CMTime time = [asset duration];
  188. int seconds = ceil(time.value/time.timescale);
  189. NSLog(@"视频时长seconds = %d",seconds);
  190. UIImage *coverImg = [self youpaifgetLocationVideoPreViewImage:[NSURL fileURLWithPath:file]];
  191. if (seconds > 60) {
  192. //视频时长大于60s将自动裁剪前60秒进行上传
  193. [self youpaifisCutVideo:file andImage:coverImg];//是否裁剪视频进行上传
  194. }else{
  195. //裁剪
  196. // YOUPAIVideoEditVC *videoEditVC = [[YOUPAIVideoEditVC alloc] init];
  197. // videoEditVC.videoUrl = [NSURL URLWithString:videoPath];
  198. // [self.navigationController pushViewController:videoEditVC animated:YES];
  199. YOUPAIZYSendVideoByLocalVC *vc = [[YOUPAIZYSendVideoByLocalVC alloc] init];
  200. vc.youpaipcoverImage = coverImg;
  201. vc.youpaipvideoFileName = file;
  202. [self pushEffectPresentToVC:vc];
  203. }
  204. }
  205. //自动裁剪视频
  206. -(void)youpaifisCutVideo:(NSString *)videoPath andImage:(UIImage *)coverImage
  207. {
  208. NSLog(@"videoPath = %@",videoPath);
  209. if(!([videoPath rangeOfString:@"://"].location != NSNotFound))
  210. {
  211. videoPath = [NSString stringWithFormat:@"file://%@",videoPath];
  212. }
  213. NSLog(@"videoPath = %@",videoPath);
  214. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"视频时长大于60秒将自动裁剪前60秒进行上传" preferredStyle:UIAlertControllerStyleAlert];
  215. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消上传" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  216. }];
  217. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定上传" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  218. [ZCHUDHelper showWithStatus:@"正在裁剪..."];
  219. NSURL *url = [NSURL URLWithString:videoPath];
  220. NSLog(@"url == %@",url);
  221. [YOUPAISHMediaManager youpaifcropWithVideoUrlStr:url start:1.0 end:60.0 completion:^(NSURL *outputURL, Float64 videoDuration, BOOL isSuccess) {
  222. if (isSuccess) {
  223. NSLog(@"-转码完成------\n路径:%@",outputURL);
  224. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  225. dispatch_async(dispatch_get_main_queue(), ^{
  226. [ZCHUDHelper showTitle:@"裁剪完成" ];
  227. YOUPAIZYSendVideoByLocalVC *vc = [[YOUPAIZYSendVideoByLocalVC alloc] init];
  228. vc.youpaipcoverImage = coverImage;
  229. vc.youpaipvideoFileName = [outputURL absoluteString];
  230. [self pushEffectPresentToVC:vc];
  231. });
  232. });
  233. }else{
  234. [ZCHUDHelper showTitle:@"裁剪失败"];
  235. }
  236. }];
  237. }];
  238. [alertController addAction:cancelAction];
  239. [alertController addAction:okAction];
  240. [self presentViewController:alertController animated:YES completion:nil];
  241. }
  242. //保存视频完成之后的回调
  243. - (void)youpaifsavedVideoPhotoImage:(UIImage*)image didFinishSavingWithError: (NSError*)error contextInfo: (void*)contextInfo {
  244. if(error) {
  245. NSLog(@"保存视频失败%@", error.localizedDescription);
  246.     }else{
  247.  NSLog(@"保存视频成功");
  248. // [self youpaifopenAlbum];
  249. }
  250. }
  251. - (void)youpaifopenAlbum{
  252. TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 columnNumber:4 delegate:nil];
  253. [LCTZImageConfigHelper setDefaultTZImageConfig:imagePickerVc];
  254. imagePickerVc.allowTakePicture = YES; // 在内部显示拍照按钮
  255. imagePickerVc.allowPickingVideo = YES;//是否允许选择视频
  256. imagePickerVc.allowPickingImage = NO;//是否允许选择照片
  257. imagePickerVc.allowPickingOriginalPhoto = NO;//是否选择原图
  258. imagePickerVc.showSelectedIndex = YES; //显示图片序号
  259. // 4. 照片排列按修改时间升序
  260. imagePickerVc.sortAscendingByModificationDate = YES;//按时间倒序排列图片
  261. // 你可以通过block或者代理,来得到用户选择的照片.
  262. // [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
  263. // YOUPAILCSendImageDynamicVC* sendDynamic = [[YOUPAILCSendImageDynamicVC alloc]init];
  264. // YOUPAILCSendDynamicModel* sendModel = [[YOUPAILCSendDynamicModel alloc]init];
  265. // sendModel.images = (NSMutableArray*)photos;
  266. // sendDynamic.sendModel = sendModel;
  267. // [self pushEffectPresentToVC:sendDynamic];
  268. // }];
  269. // [imagePickerVc setDidFinishPickingGifImageHandle:^(UIImage *animatedImage, id sourceAssets) {
  270. // YOUPAILCSendImageDynamicVC* sendDynamic = [[YOUPAILCSendImageDynamicVC alloc]init];
  271. // YOUPAILCSendDynamicModel* sendModel = [[YOUPAILCSendDynamicModel alloc]init];
  272. // sendModel.images = [NSMutableArray arrayWithObject:animatedImage];
  273. // sendDynamic.sendModel = sendModel;
  274. // [self pushEffectPresentToVC:sendDynamic];
  275. // }];
  276. [imagePickerVc setDidFinishPickingVideoHandle:^(UIImage *coverImage, PHAsset *asset) {
  277. PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
  278. options.version = PHVideoRequestOptionsVersionOriginal;
  279. options.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
  280. options.networkAccessAllowed = YES;
  281. [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
  282. AVURLAsset *urlAsser = ( AVURLAsset *)asset;
  283. NSString *videoPath =[urlAsser.URL absoluteString];
  284. NSLog(@"%@",videoPath);
  285. NSLog(@"%@",info);
  286. // [LCHttpHelper requestWithURLString:TrendsSave parameters:@{@"videoFileName":videoPath,@"content":self.liveView.textView.text} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  287. // NSDictionary* dict = (NSDictionary*)responseObject;
  288. // NSInteger code = [[dict objectForKey:@"code"] integerValue];
  289. // if (code==0) {//成功
  290. //
  291. // }
  292. // } failure:^(NSError *error) {
  293. //
  294. // }];
  295. ///Users/elaine/Library/Developer/CoreSimulator/Devices/BEF0EFD4-9111-484B-861C-ACAF6CDB3E3E/data/Containers/Data/Application/FD1239FA-D00D-4A57-B870-5B00D9FC3C29/Library/Caches/e011cd379dde4be396222e95988f23fd.mp4
  296. //file:///Users/elaine/Library/Developer/CoreSimulator/Devices/BEF0EFD4-9111-484B-861C-ACAF6CDB3E3E/data/Media/DCIM/100APPLE/IMG_0024.mp4
  297. }];
  298. }];
  299. [self presentViewController:imagePickerVc animated:YES completion:nil];
  300. }
  301. // 获取本地视频第一帧
  302. - (UIImage*) youpaifgetLocationVideoPreViewImage:(NSURL *)path
  303. {
  304. AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:path options:nil];
  305. AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
  306. assetGen.appliesPreferredTrackTransform = YES;
  307. CMTime time = CMTimeMakeWithSeconds(0.0, 600);
  308. NSError *error = nil;
  309. CMTime actualTime;
  310. CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error];
  311. UIImage *videoImage = [[UIImage alloc] initWithCGImage:image];
  312. CGImageRelease(image);
  313. return videoImage;
  314. }
  315. //隐藏键盘
  316. -(void)youpaifhideTheKeyboard{
  317. dispatch_async(dispatch_get_main_queue(), ^{
  318. [self.youpaiptextField endEditing:YES];
  319. });
  320. if (self.youpaiptextField.text.length != 0) {
  321. self.youpaipsendVideoBtn.userInteractionEnabled = YES;
  322. [self.youpaipsendVideoBtn setBackgroundImage:[LCTools ColorImage:self.youpaipsendVideoBtn.frame.size FromColors:@[ZYGradientOneColor,ZYGradientTwoColor] ByGradientType:(GradientLeftToRight)] forState:(UIControlStateNormal)];
  323. }else{
  324. self.youpaipsendVideoBtn.userInteractionEnabled = NO;
  325. [self.youpaipsendVideoBtn setBackgroundImage:nil forState:(UIControlStateNormal)];
  326. }
  327. }
  328. @end