CreateLiveViewController.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // CreateLiveViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/30.
  6. //
  7. #import "CreateLiveViewController.h"
  8. #import "CreateLiveBodyView.h"
  9. #import "CustomNavViewController.h"
  10. #import "LiveRoomViewController.h"
  11. #import "KSChoosePicker.h"
  12. #import "KSMediaManager.h"
  13. @interface CreateLiveViewController ()
  14. @property (nonatomic, strong) CreateLiveBodyView *bodyView;
  15. @property (nonatomic, strong) NSString *chooseTime;
  16. @property (nonatomic, strong) KSMediaManager *mediaManager;
  17. @property (nonatomic, strong) NSMutableArray *imageArray; // 图片数组
  18. @property (nonatomic, strong) NSMutableArray *imageAsset; // 图片 asset
  19. @end
  20. @implementation CreateLiveViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. // Do any additional setup after loading the view.
  24. [self allocTitle:@"创建直播"];
  25. [self configUI];
  26. }
  27. - (void)viewWillAppear:(BOOL)animated {
  28. [super viewWillAppear:animated];
  29. if ([self checkIsLoginToLoginView:YES]) {
  30. }
  31. }
  32. - (void)configUI {
  33. self.bodyView = [CreateLiveBodyView shareInstance];
  34. [self.scrollView addSubview:self.bodyView];
  35. CGFloat height = kScreenHeight -kNaviBarHeight - iPhoneXSafeBottomMargin > 770 ? kScreenHeight -kNaviBarHeight - iPhoneXSafeBottomMargin : 770;
  36. [self.bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.top.mas_equalTo(self.scrollView.mas_top);
  38. make.left.right.mas_equalTo(self.view);
  39. make.height.mas_equalTo(height);
  40. }];
  41. [self.scrollView setContentSize:CGSizeMake(kScreenWidth, height)];
  42. MJWeakSelf;
  43. [self.bodyView createSureCallback:^(BOOL chooseTime, NSString * _Nonnull title, NSString * _Nonnull content) {
  44. if (chooseTime) {
  45. [weakSelf chooseTimeAction];
  46. }
  47. else {
  48. [weakSelf createLiveAction:title content:content];
  49. }
  50. }];
  51. [self.bodyView chooseCoverCallback:^{
  52. [weakSelf pushImagePickerController];
  53. }];
  54. }
  55. - (void)pushImagePickerController {
  56. // 调用相册
  57. self.mediaManager = [[KSMediaManager alloc] init];
  58. self.mediaManager.mediaType = MEDIATYPE_PHOTO;
  59. self.mediaManager.maxPhotoNumber = 1;
  60. self.mediaManager.baseCtrl = self;
  61. self.mediaManager.needCropImage = YES;
  62. MJWeakSelf;
  63. [self.mediaManager noAlertCallback:^(NSString * _Nullable videoUrl, NSMutableArray * _Nullable imageArray, NSMutableArray * _Nullable imageAsset) {
  64. [weakSelf displayChooseImage:imageArray imageAsset:imageAsset];
  65. }];
  66. [self.mediaManager pushImagePickerController];
  67. }
  68. - (void)displayChooseImage:(NSMutableArray *)imageArray imageAsset:(NSMutableArray *)imageAsset {
  69. // 添加图片
  70. self.imageArray = [imageArray mutableCopy];
  71. self.imageAsset = [imageAsset mutableCopy];
  72. // 重新布局
  73. [self.bodyView.coverImage setImage:[self.imageArray lastObject]];
  74. self.bodyView.coverBgView.hidden = NO;
  75. }
  76. - (void)chooseTimeAction {
  77. MJWeakSelf;
  78. KSChoosePicker *picker = [[KSChoosePicker alloc] initWithTitle:@"直播时长(分钟)" sourceData:@[@"30",@"45",@"60",@"90"] chooseReturnWithBlock:^(NSString * _Nonnull returnValue, NSInteger chooseIndex) {
  79. weakSelf.chooseTime = returnValue;
  80. weakSelf.bodyView.timeLabel.text = [NSString stringWithFormat:@"%@分钟",returnValue];
  81. } cancel:^{
  82. }];
  83. [picker showPicker];
  84. }
  85. - (void)createLiveAction:(NSString *)title content:(NSString *)content {
  86. if (self.imageArray.count == 0) {
  87. [self MBPShow:@"请上传直播封面图"];
  88. return;
  89. }
  90. [self showhud];
  91. UIImage *coverImage = [self.imageArray lastObject];
  92. NSData *imgData = [UIImage turnsImaegDataByImage:coverImage];
  93. NSString *fileName = @"image";
  94. [[KSUploadManager shareInstance] configBucketName:@"daya"];
  95. [[KSUploadManager shareInstance] uploadImage:imgData fileName:fileName successCallback:^(NSMutableArray * _Nonnull fileUrlArray) {
  96. NSString *avatarUrl = [fileUrlArray lastObject];
  97. [self uploadCoverImage:avatarUrl liveTilte:title content:content];
  98. } faliure:^(NSError * _Nullable error, NSString *descMessaeg) {
  99. [self removehub];
  100. if (![NSString isEmptyString:descMessaeg]) {
  101. [self MBPShow:descMessaeg];
  102. }
  103. }];
  104. }
  105. - (void)uploadCoverImage:(NSString *)coverImageUrl liveTilte:(NSString *)title content:(NSString *)content {
  106. [KSNetworkingManager liveRoomCreateTempLiveRoom:KS_POST coverPic:coverImageUrl liveRemark:content roomTitle:title liveTime:[self.chooseTime integerValue] success:^(NSDictionary * _Nonnull dic) {
  107. [self removehub];
  108. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  109. NSString *roomId = [dic ks_stringValueForKey:@"data"];
  110. [self previewViewWithRoomId:roomId liveDesc:content];
  111. }
  112. else {
  113. [self MBPShow:MESSAGEKEY];
  114. }
  115. } faliure:^(NSError * _Nonnull error) {
  116. [self removehub];
  117. }];
  118. }
  119. - (void)previewViewWithRoomId:(NSString *)roomId liveDesc:(NSString *)liveDesc {
  120. LiveRoomViewController *ctrl = [[LiveRoomViewController alloc] init];
  121. ctrl.roomId = roomId;
  122. ctrl.isTempRoom = YES;
  123. ctrl.liveContent = liveDesc;
  124. MJWeakSelf;
  125. [ctrl backRefreshCallback:^{
  126. [weakSelf backAction];
  127. }];
  128. CustomNavViewController *navCtrl = [[CustomNavViewController alloc] initWithRootViewController:ctrl];
  129. navCtrl.modalPresentationStyle = UIModalPresentationFullScreen;
  130. [self.navigationController presentViewController:navCtrl animated:YES completion:nil];
  131. }
  132. #pragma mark - 懒加载
  133. - (NSMutableArray *)imageArray {
  134. if (!_imageArray) {
  135. _imageArray = [NSMutableArray array];
  136. }
  137. return _imageArray;
  138. }
  139. - (NSMutableArray *)imageAsset {
  140. if (!_imageAsset) {
  141. _imageAsset = [NSMutableArray array];
  142. }
  143. return _imageAsset;
  144. }
  145. /*
  146. #pragma mark - Navigation
  147. // In a storyboard-based application, you will often want to do a little preparation before navigation
  148. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  149. // Get the new view controller using [segue destinationViewController].
  150. // Pass the selected object to the new view controller.
  151. }
  152. */
  153. @end