// // DDDynamicSendViewController.m // DingDangApp // // Created by apple on 2023/6/2. // #import "DDDynamicSendViewController.h" #import "ZLShowMultimedia.h" #import "ZLPhotoActionSheet.h" #import "ZLPhotoManager.h" #import "DDDynamicAddPicView.h" #import "DDDynamicPicItemView.h" #import "FileToolsObject.h" #import "DynamicSendLocationView.h" #import "TZLocationManager.h" #import "UITextView+Placeholder.h" #import "Api+DDDynamicApi.h" @interface DDDynamicSendViewController () // 1:文本 2:视频 3:图片 @property (nonatomic,assign) NSInteger releaseType; @property (nonatomic,assign) BOOL isUploading; @property (nonatomic,strong) UIScrollView * dynamicScrollView; @property (nonatomic,strong) DDDynamicAddPicView * itemList; @property (nonatomic, strong) DDDynamicPicItemView *item; @property (nonatomic, strong) DynamicSendLocationView * locationView; //资源数组 @property (nonatomic, strong) NSArray * SelectedImgArr; //本地图片数组 @property (nonatomic, strong) NSMutableArray * imageFileArr; //本地图片数组 @property (nonatomic, strong) NSMutableArray * imageArr; //视频本地首帧 @property (nonatomic,copy)NSString *VideoThumbUrl; //视频本地地址 @property(nonatomic,copy)NSString *VideofileUrl; //视频首帧 @property (nonatomic,copy)NSString *ReleaseVideoThumbUrl; //视频地址 @property(nonatomic,copy)NSString *ReleaseVideoUrl; //宽高比 @property (nonatomic, assign) CGFloat proportion; @property (nonatomic, strong) ZLPhotoActionSheet *photoActionSheet; @property (nonatomic,strong) UITextView * textView; @property (nonatomic,strong) UILabel * textCountLabel; @property (nonatomic, strong) UIButton * cancelBtn; @property (nonatomic, assign) BOOL isLocation; @property (nonatomic,assign) CLAuthorizationStatus authStatus; @end @implementation DDDynamicSendViewController - (void)appDidBecomeActiveNotification { if ([CLLocationManager authorizationStatus] == self.authStatus || self.authStatus == -1) { return; } [self getLocation]; } -(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; } -(DynamicSendLocationView *)locationView { if (!_locationView) { _locationView = [[DynamicSendLocationView alloc]initWithFrame:CGRectMake(0, self.itemList.y + 10, kWidth, 55)]; WeakSelf(weakSelf) _locationView.locationSwitchChangeValueBlock = ^(BOOL isLocation) { if (isLocation && [AppDelegate getAppdelegate].isLocation == NO) { weakSelf.locationView.itemSwitch.on = NO; NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if([[UIApplication sharedApplication] canOpenURL:url]) { NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString]; if (@available(iOS 10.0, *)) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) { }]; } else { [[UIApplication sharedApplication] openURL:url]; } } }else{ weakSelf.isLocation = isLocation; } }; } return _locationView; } - (UITextView *)textView{ if (!_textView){ _textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, kWidth-20, 200)]; _textView.textColor = [UIColor jk_colorWithHexString:@"#333333"]; _textView.placeholder = @"请输入内容~"; _textView.placeholderColor = [UIColor jk_colorWithHexString:@"#999999"]; _textView.font = [UIFont systemFontOfSize:14]; _textView.delegate = self; _textView.layer.cornerRadius = 7; _textView.clipsToBounds = YES; _textView.userInteractionEnabled = YES; }return _textView; } - (UILabel *)textCountLabel{ if (!_textCountLabel){ _textCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.textView.bottom+5, kWidth-20, 40)]; _textCountLabel.textColor = [UIColor jk_colorWithHexString:@"#666666"]; _textCountLabel.font = [UIFont boldSystemFontOfSize:14]; _textCountLabel.text = @"0/300"; _textCountLabel.textAlignment = NSTextAlignmentRight; }return _textCountLabel; } - (void)textViewDidChange:(UITextView *)textView{ self.textCountLabel.text = [NSString stringWithFormat:@"%lu/300",[textView.text length]]; } - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text]; if (str.length > 300) { return NO; } return YES; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActiveNotification) name:UIApplicationDidBecomeActiveNotification object:nil]; [self getLocation]; [self defaultNavTitle:@"发布动态" hideLine:NO]; [self.customNavBar wr_setRightButtonWithTitle:@"发布" titleColor:[UIColor jk_colorWithHexString:@"#333333"]]; [self.customNavBar wr_setLeftButtonWithTitle:@"取消" titleColor:[UIColor jk_colorWithHexString:@"#333333"]]; WeakSelf(weakSelf) self.customNavBar.onClickRightButton = ^{ if (weakSelf.isUploading) { [ToolsObject addPopVieToText:@"正在发布中,请不要重复提交"]; return; } [weakSelf.view endEditing:YES]; [weakSelf releaseDynamic]; }; self.customNavBar.onClickLeftButton = ^{ [weakSelf.navigationController popViewControllerAnimated:YES]; }; self.view.backgroundColor = UIColor.whiteColor; [self.view addSubview:self.dynamicScrollView]; [self.dynamicScrollView addSubview:self.textView]; [self.dynamicScrollView addSubview:self.textCountLabel]; [self.dynamicScrollView addSubview:self.itemList]; [self.itemList addItem:self.item]; [self.dynamicScrollView addSubview:self.locationView]; [self updateHeaderViewHeight]; self.item.playButtonClick = ^{ ZLShowMultimedia *media = [[ZLShowMultimedia alloc] init]; NSMutableArray *infos = [NSMutableArray new]; ZLMediaInfo *info = [[ZLMediaInfo alloc] init]; info.type = ZLMediaInfoTypeVideo; info.localVideoUrl = weakSelf.VideofileUrl; info.isLocal = YES; [infos addObject:info]; media.infos = infos; media.currentIndex = 0; [media show]; }; } - (void)getLocation { [[TZLocationManager manager] startLocation]; self.authStatus = [CLLocationManager authorizationStatus]; if ([CLLocationManager locationServicesEnabled] == NO) { self.authStatus = -1; return; } if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted) { [AppDelegate getAppdelegate].isLocation = NO; self.isLocation = NO; self.locationView.itemSwitch.on = [AppDelegate getAppdelegate].isLocation; self.isLocation = [AppDelegate getAppdelegate].isLocation; [ToolsObject ddUploadMyLocation]; } WeakSelf(weakSelf) [[TZLocationManager manager] startLocationWithSuccessBlock:^(NSArray *locations) { } failureBlock:^(NSError *error) { } geocoderBlock:^(NSArray *geocoderArray) { if (geocoderArray.count > 0) { CLPlacemark * placemark = geocoderArray[0]; [AppDelegate getAppdelegate].longitude = [NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude]; [AppDelegate getAppdelegate].latitude = [NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude]; [AppDelegate getAppdelegate].locationCity = [NSString stringWithFormat:@"%@",placemark.locality]; [AppDelegate getAppdelegate].provinceStr = [NSString stringWithFormat:@"%@",placemark.administrativeArea]; [AppDelegate getAppdelegate].countyStr = @""; [AppDelegate getAppdelegate].addressStr = [NSString stringWithFormat:@"%@%@%@",[ToolsObject IsNullWithObject:[AppDelegate getAppdelegate].provinceStr]?@"":[AppDelegate getAppdelegate].provinceStr,[ToolsObject IsNullWithObject:[AppDelegate getAppdelegate].locationCity]?@"":[AppDelegate getAppdelegate].locationCity,placemark.name]; weakSelf.locationView.cityLabel.text = [AppDelegate getAppdelegate].isLocation ? [AppDelegate getAppdelegate].locationCity:@""; if (![AppDelegate getAppdelegate].isLocation) {//多次调用,只执行一次 [AppDelegate getAppdelegate].isLocation = YES; weakSelf.isLocation = YES; weakSelf.locationView.itemSwitch.on = [AppDelegate getAppdelegate].isLocation; weakSelf.isLocation = [AppDelegate getAppdelegate].isLocation; [ToolsObject ddUploadMyLocation]; } } }]; } /* #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. } */ -(void)uploadImg:(NSArray *)image andimage:(NSArray *)imageArr{ if ([ToolsObject IsNullWithObject:image]) { return; } self.SelectedImgArr = image; PHAsset *asset = image[0]; if (asset.mediaType == PHAssetMediaTypeVideo) { self.releaseType = 2; if (imageArr.count != 0) { for (UIImage *images in imageArr) { self.item.itemImage.image = images; self.item.playButton.center = CGPointMake(self.item.size.width/2, self.item.size.height/2); self.item.playButton.hidden = NO; self.item.isAdd = NO; self.item.removeButton.hidden = NO; self.itemList.maxItem = 1; [self.itemList addItem:self.item]; } } PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init]; options.version = PHImageRequestOptionsVersionCurrent; options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic; WeakSelf(weakSelf) [ZLPhotoManager requestAssetFileUrl:asset complete:^(NSString * _Nonnull filePath) { NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey]; AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:filePath] options:opts]; NSString *path = [FileToolsObject libraryDir]; dispatch_async(dispatch_get_main_queue(), ^{ UIImage *coverImage = [ToolsObject getVideoFirstViewImage:[NSURL URLWithString:filePath]]; weakSelf.proportion = floor((coverImage.size.width/coverImage.size.height)*100)/100; NSString * thumbImagePath = [NSString stringWithFormat:@"source/Dynamic/video/%@.jpg",[ToolsObject dateChangeRealtimestamp]]; weakSelf.VideoThumbUrl = [path stringByAppendingPathComponent:thumbImagePath]; [FileToolsObject createFileAtPath:weakSelf.VideoThumbUrl overwrite:YES]; NSData * thumbImageData =UIImageJPEGRepresentation(coverImage, 1.0); [FileToolsObject writeFileAtPath:weakSelf.VideoThumbUrl content:thumbImageData]; }); dispatch_async(dispatch_get_main_queue(), ^{ @autoreleasepool { [ToolsObject dd_movieFileMP4:urlAsset.URL completion:^(NSURL *Mp4FilePath) { NSString * thumbImagePath1 = [NSString stringWithFormat:@"source/Dynamic/video/%@.mp4",[ToolsObject dateChangeRealtimestamp]]; weakSelf.VideofileUrl = [path stringByAppendingPathComponent:thumbImagePath1]; [FileToolsObject createFileAtPath:weakSelf.VideofileUrl overwrite:YES]; NSData * mp4Data = [NSData dataWithContentsOfURL:urlAsset.URL]; [FileToolsObject writeFileAtPath:weakSelf.VideofileUrl content:mp4Data]; }]; } }); }]; }else if (asset.mediaType == PHAssetMediaTypeImage){ self.releaseType = 3; self.item.playButton.hidden = YES; self.item.isAdd = YES; self.itemList.maxItem = 9; if (imageArr.count != 0) { for (UIImage *image in imageArr) { DDDynamicPicItemView *item = [[DDDynamicPicItemView alloc] init]; item.itemImage.image = image; item.removeButton.hidden = NO; [self.itemList addItem:item]; } } [self updateHeaderViewHeight]; } } #pragma mark ---发动态 -(void)releaseDynamic { if ([ToolsObject IsNullWithObject:self.textView.text]) { [ToolsObject addPopVieToText:@"请输入文字"]; return; } // [ToolsObject ShowSVProgressHUD:@"上传中"]; // WeakSelf(weakSelf) // if (self.itemList.itemArray.count == 1 && self.releaseType == 2) { // [ToolsObject uploadImageWithArr:@[self.VideoThumbUrl].mutableCopy WithRecourseType:(OSSUploadType_DynamicImage) completionHandler:^(NSMutableArray * _Nonnull urlArr) { // weakSelf.ReleaseVideoThumbUrl = urlArr.firstObject; // [ToolsObject uploadVideoWithFileUrl:weakSelf.VideofileUrl WithRecourseType:(OSSUploadType_DynamicVideo) completionHandler:^(id _Nonnull urlString) { // [ToolsObject DismissSVProgressHUD]; // weakSelf.ReleaseVideoUrl = urlString; // [weakSelf publishReuqest]; // return; // }]; // }]; // return; // } // if (self.itemList.itemArray.count >0 && self.releaseType == 3) { // NSString *path = [FileToolsObject libraryDir]; // NSMutableArray *array = [[NSMutableArray alloc] initWithArray:self.itemList.itemArray]; // [self.imageFileArr removeAllObjects]; // // for (int i = 0; i0) { NSString *imgStr = @""; for (int i = 0; i * _Nonnull images, NSArray * _Nonnull assets, BOOL isOriginal) { [weakSelf uploadImg:assets andimage:images]; }]; } return _photoActionSheet; } -(NSMutableArray *)imageFileArr { if (!_imageFileArr) { _imageFileArr = [NSMutableArray array]; } return _imageFileArr; } -(NSMutableArray *)imageArr { if (!_imageArr) { _imageArr = [NSMutableArray array]; } return _imageArr; } - (UIScrollView *)dynamicScrollView{ if (!_dynamicScrollView){ _dynamicScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, DDNavigationBarHeight(), kWidth, kHeight - DDNavigationBarHeight()-KTABBAR_H-20)]; _dynamicScrollView.backgroundColor = UIColor.whiteColor; }return _dynamicScrollView; } @end