| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- //
- // UploadVideoViewController.m
- // model
- //
- // Created by Drew on 2019/1/3.
- // Copyright © 2019 Mine. All rights reserved.
- //
- #import <MediaPlayer/MediaPlayer.h>
- #import "UploadVideoViewController.h"
- #import "TOCropViewController.h"
- #import <Photos/Photos.h>
- #import "AFNetworking.h"
- @interface UploadVideoViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, TOCropViewControllerDelegate>
- @property(weak, nonatomic) IBOutlet UITextField *descText;
- @property(weak, nonatomic) IBOutlet UIButton *playBtn;
- @property(weak, nonatomic) IBOutlet UIButton *coverBtn;
- @property(weak, nonatomic) IBOutlet UIImageView *coverImg;
- @property(weak, nonatomic) IBOutlet UIView *videoView;
- @property(nonatomic, strong) NSURL *videoURL;
- @property(nonatomic, strong) NSString *isSgined;
- @end
- @implementation UploadVideoViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.navigationItem.title = @"活动报名";
- UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
- self.navigationItem.leftBarButtonItem = leftItem;
- self.navigationController.navigationBar.tintColor = [UIColor blackColor];
- [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
- switch (status) {
- case PHAuthorizationStatusAuthorized:
- NSLog(@"PHAuthorizationStatusAuthorized");
- break;
- case PHAuthorizationStatusDenied:
- NSLog(@"PHAuthorizationStatusDenied");
- break;
- case PHAuthorizationStatusNotDetermined:
- NSLog(@"PHAuthorizationStatusNotDetermined");
- break;
- case PHAuthorizationStatusRestricted:
- NSLog(@"PHAuthorizationStatusRestricted");
- break;
- }
- }];
- self.isSgined = @"0";
- NSString *url = [NSString stringWithFormat:@"%@/activity?action=getactivity&pk=%@&memberpk=%@", PublicUrl, self.activitypk, [Helper sharedAccount].accid];
- [[AHHttpManager sharedManager]
- POST:url
- parameters:nil
- success:^(id responseObject) {
- if ([@"success" isEqualToString:responseObject[@"msg"]]) {
- NSDictionary *data = responseObject[@"data"];
- self.isSgined = [data[@"signed"] stringValue];
- ActivityModel *model = [[ActivityModel alloc] initWithDictionary:data error:nil];
- self.model = model;
- }
- }
- failure:^(NSError *error) {
- }];
- }
- - (void)backClick {
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- self.navigationController.navigationBar.tintColor = [UIColor blackColor];
- [self.navigationController.navigationBar setShadowImage:[UIImage new]];
- [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
- [self.navigationController.navigationBar setShadowImage:nil];
- }
- - (IBAction)chooseVideo:(id)sender {
- UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
- pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- pickerController.mediaTypes = @[(NSString *) kUTTypeMovie];
- pickerController.allowsEditing = YES;
- pickerController.videoQuality = UIImagePickerControllerQualityTypeMedium;//视频质量
- pickerController.videoMaximumDuration = 30.f;//视频最长长度
- pickerController.delegate = self;
- [self.navigationController presentViewController:pickerController animated:YES completion:nil];
- }
- - (IBAction)chooseCover:(id)sender {
- UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
- pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;//图片分组列表样式
- pickerController.delegate = self;
- [self.navigationController presentViewController:pickerController animated:YES completion:nil];
- }
- - (IBAction)play:(id)sender {
- if (self.videoURL) {
- MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:self.videoURL];
- playerViewController.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
- [self presentMoviePlayerViewControllerAnimated:playerViewController];
- }
- }
- - (IBAction)confirm:(id)sender {
- if (!self.model) {
- return;
- }
- if ([self.isSgined intValue] > 0) {
- [MBProgressHUD showInfo:@"请勿重复报名"];
- return;
- }
- if (!self.videoURL) {
- [MBProgressHUD showInfo:@"请选择视频"];
- } else if (!self.descText.text.length) {
- [MBProgressHUD showInfo:@"请填写描述"];
- } else {
- MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
- hud.label.text = @"正在上传";
- hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
- hud.removeFromSuperViewOnHide = YES;
- NSString *parttypk = self.model.pk;
- NSString *pk = [ModelUser user].pk;
- 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];
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
- manager.requestSerializer = [AFJSONRequestSerializer serializer];
- manager.securityPolicy.allowInvalidCertificates = YES;
- [manager POST:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:@{} constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
- NSData *imgData = UIImageJPEGRepresentation(self.coverImg.image, 0.5);
- [formData appendPartWithFormData:imgData name:@"image"];
- [formData appendPartWithFileData:imgData name:@"image" fileName:@"image.jpg" mimeType:@"image/jpeg"];
- NSData *videoData = [NSData dataWithContentsOfURL:self.videoURL];
- [formData appendPartWithFileData:videoData name:@"video" fileName:@"video.mov" mimeType:@"video/quicktime"];
- } progress:^(NSProgress *uploadProgress) {
- NSLog(@"progress %lli / %lli", uploadProgress.completedUnitCount, uploadProgress.totalUnitCount);
- hud.progress = uploadProgress.completedUnitCount / (float) uploadProgress.totalUnitCount;
- } success:^(NSURLSessionDataTask *task, id responseObject) {
- [hud hideAnimated:YES];
- [MBProgressHUD showInfo:@"报名成功"];
- [self.navigationController popToRootViewControllerAnimated:YES];
- } failure:^(NSURLSessionDataTask *task, NSError *error) {
- [hud hideAnimated:YES];
- NSLog(@"error:%@", error);
- [MBProgressHUD showInfo:@"上传失败,请稍后再试"];
- }];
- }
- }
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {
- if (picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum) {
- UIImage *image = info[UIImagePickerControllerOriginalImage];
- TOCropViewController *cropViewController = [[TOCropViewController alloc] initWithImage:image];
- cropViewController.delegate = self;
- cropViewController.customAspectRatio = CGSizeMake(300, 300);
- cropViewController.aspectRatioLockEnabled = YES;
- cropViewController.resetAspectRatioEnabled = NO;
- [picker dismissViewControllerAnimated:YES completion:nil];
- [self presentViewController:cropViewController animated:YES completion:nil];
- } else {
- NSURL *url = info[UIImagePickerControllerMediaURL];//获得视频的URL
- NSLog(@"url %@", url);
- self.videoURL = url;
- // MPMoviePlayerController *playerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
- // playerController.view.frame = CGRectMake(0, 0, self.videoView.frame.size.width, self.videoView.frame.size.height);
- // [self.videoView addSubview:playerController.view];//第四步:设置播放器属性
- // playerController.controlStyle = MPMovieControlStyleDefault;
- // playerController.shouldAutoplay = NO;
- // playerController.scalingMode = MPMovieScalingModeAspectFit;
- // playerController.repeatMode = MPMovieRepeatModeOne;
- // [playerController prepareToPlay];
- // [playerController play];
- [picker dismissViewControllerAnimated:YES completion:nil];
- AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
- CMTime audioDuration = asset.duration;
- NSNumber *duration = @([@(CMTimeGetSeconds(audioDuration)) intValue]);
- 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 *thumbnail = [[UIImage alloc] initWithCGImage:image];
- CGImageRelease(image);
- self.coverImg.image = thumbnail;
- }
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
- - (void)cropViewController:(TOCropViewController *)cropViewController didCropToImage:(UIImage *)image withRect:(CGRect)cropRect angle:(NSInteger)angle {
- self.coverImg.image = image;
- [cropViewController dismissViewControllerAnimated:YES completion:nil];
- }
- @end
|