PublishViewController.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // PublishViewController.m
  3. // model
  4. //
  5. // Created by liufei on 2018/7/25.
  6. // Copyright © 2018年 Mine. All rights reserved.
  7. //
  8. #import "PublishViewController.h"
  9. #import "UITextView+WZB.h"
  10. #import "PhotoCollectionViewCell.h"
  11. @interface PublishViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
  12. {
  13. UIImagePickerController *pickerVc;
  14. }
  15. @property (nonatomic, strong) UITextView *pubTextView;
  16. @property (nonatomic, strong) NSMutableArray *dataSource;
  17. @property (nonatomic, strong) UICollectionView *photoListView;
  18. @property (nonatomic, strong) NSMutableArray *imageSource;
  19. @end
  20. @implementation PublishViewController
  21. - (void)viewWillAppear:(BOOL)animated {
  22. [super viewWillAppear:animated];
  23. // [self.navigationController.navigationBar setShadowImage:[UIImage new]];
  24. self.navigationController.navigationBar.translucent = NO;
  25. }
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. self.imageSource = [NSMutableArray new];
  29. self.dataSource = [NSMutableArray new];
  30. [self configUI];
  31. // Do any additional setup after loading the view.
  32. }
  33. - (void)configUI {
  34. self.view.backgroundColor = [UIColor whiteColor];
  35. self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
  36. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(handleCancel)];
  37. [self.navigationItem.leftBarButtonItem setTintColor:[UIColor blackColor]];
  38. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发表" style:UIBarButtonItemStylePlain target:self action:@selector(handlePublish)];
  39. [self.navigationItem.rightBarButtonItem setTintColor:[UIColor colorWithHexString:@"#fe4086"]];
  40. self.pubTextView = [[UITextView alloc] initWithFrame:CGRectMake(40, 40, ScreenWidth - 80, 76)];
  41. self.pubTextView.wzb_placeholder = @"这一刻的想法...";
  42. self.pubTextView.font = [UIFont systemFontOfSize:16];
  43. [self.view addSubview:self.pubTextView];
  44. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  45. layout.sectionInset = UIEdgeInsetsMake(0, 40, 10, 40);
  46. layout.itemSize = CGSizeMake(ScreenWidth/3 - 30, ScreenWidth/3 - 30);
  47. layout.minimumLineSpacing = 5;
  48. layout.minimumInteritemSpacing = 5;
  49. self.photoListView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.pubTextView.frame), ScreenWidth, ScreenWidth *2/3 - 50) collectionViewLayout:layout];
  50. self.photoListView.delegate = self;
  51. self.photoListView.dataSource = self;
  52. self.photoListView.backgroundColor = [UIColor whiteColor];
  53. [self.photoListView registerNib:[UINib nibWithNibName:@"PhotoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([PhotoCollectionViewCell class])];
  54. [self.view addSubview:self.photoListView];
  55. }
  56. #pragma mark -
  57. - (void)handleSave {
  58. }
  59. - (void)handleCancel{
  60. [self.navigationController popViewControllerAnimated:YES];
  61. }
  62. - (void)handlePublish {
  63. if (self.pubTextView.text.length==0&&self.imageSource.count==0) {
  64. [MBProgressHUD showTextHUD:@"请输入动态内容" inView:self.view hideAfterDelay:1];
  65. }else{
  66. [self firstPublish];
  67. }
  68. }
  69. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  70. if (![self.pubTextView isExclusiveTouch]) {
  71. [self.pubTextView resignFirstResponder];
  72. }
  73. }
  74. - (void)firstPublish{
  75. NSString *str = [NSString stringWithFormat:@"%@modelInfo?action=doAddPrivate&ModelPK=%@&Intro=%@",imageURl,[ModelUser user].modelpk,self.pubTextView.text];
  76. [[AHHttpManager sharedManager]POST:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil success:^(id responseObject) {
  77. NSLog(@"%@",responseObject);
  78. if ([[responseObject objectForKey:@"msg"]isEqualToString:@"success"]) {
  79. NSString *privatePk = [responseObject objectForKey:@"privatepk"];
  80. if(self.imageSource.count==0){
  81. [self.navigationController popViewControllerAnimated:YES];
  82. }else{
  83. [self haveImagePublish:privatePk];
  84. }
  85. }else{
  86. [MBProgressHUD showTextHUD:@"超时" inView:self.view hideAfterDelay:1];
  87. }
  88. } failure:^(NSError *error) {
  89. NSLog(@"error = %@",error);
  90. [MBProgressHUD showTextHUD:@"超时" inView:self.view hideAfterDelay:1];
  91. }];
  92. }
  93. - (void)haveImagePublish:(NSString*)privatePk{
  94. NSString * urlStr;
  95. for (int i=0; i<self.imageSource.count; i++) {
  96. if (i==0) {
  97. urlStr = self.imageSource[0];
  98. }else{
  99. urlStr = [NSString stringWithFormat:@"%@,%@",urlStr,self.imageSource[i]];
  100. }
  101. }
  102. NSString *str = [NSString stringWithFormat:@"%@modelInfo?action=doAddPrivateItem&ModelPK=%@&PrivatePK=%@&URL=%@&FType=图片",imageURl,[ModelUser user].modelpk,privatePk,urlStr];
  103. [[AHHttpManager sharedManager]POST:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil success:^(id responseObject) {
  104. NSLog(@"%@",responseObject);
  105. if ([[responseObject objectForKey:@"msg"]isEqualToString:@"success"]) {
  106. [self.navigationController popViewControllerAnimated:YES];
  107. }else{
  108. [MBProgressHUD showTextHUD:@"超时" inView:self.view hideAfterDelay:1];
  109. }
  110. } failure:^(NSError *error) {
  111. NSLog(@"error = %@",error);
  112. [MBProgressHUD showTextHUD:@"超时" inView:self.view hideAfterDelay:1];
  113. }];
  114. }
  115. #pragma mark -
  116. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  117. return self.dataSource.count < 4 ? self.dataSource.count + 1:4;
  118. }
  119. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  120. PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([PhotoCollectionViewCell class]) forIndexPath:indexPath];
  121. if (indexPath.item < self.dataSource.count) {
  122. cell.userImageView.image = self.dataSource[indexPath.item];
  123. }
  124. return cell;
  125. }
  126. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  127. if (self.dataSource.count == indexPath.item) {
  128. UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  129. pickerVc = [[UIImagePickerController alloc] init];
  130. pickerVc.allowsEditing = YES;
  131. pickerVc.delegate = self;
  132. UIAlertAction *library = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  133. pickerVc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  134. [self presentViewController:pickerVc animated:YES completion:nil];
  135. }];
  136. UIAlertAction *camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  137. pickerVc.sourceType = UIImagePickerControllerSourceTypeCamera;
  138. [self presentViewController:pickerVc animated:YES completion:nil];
  139. }];
  140. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  141. [alertVc addAction:library];
  142. [alertVc addAction:camera];
  143. [alertVc addAction:cancel];
  144. [self presentViewController:alertVc animated:YES completion:nil];
  145. }
  146. }
  147. #pragma mark -
  148. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
  149. UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
  150. [picker dismissViewControllerAnimated:NO completion:nil];
  151. [self postPicDataWithNetworkingWith:image];
  152. }
  153. #pragma mark -- 上传图片
  154. - (void)postPicDataWithNetworkingWith:(UIImage *)image {
  155. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  156. [YanCNetWorkManager requestPostWithURLStr:Url_skillphoto(PublicUrl) fileData:image name:@"img" fileName:@"111.png" mimeType:@"image/jpg/png" parameters:nil finish:^(id dataDic) {
  157. [MBProgressHUD hideHUDForView:self.view animated:YES];
  158. NSString *issuccess = dataDic[@"msg"];
  159. if ([issuccess isEqualToString:@"success"]) {
  160. NSString *pubImageUrl = dataDic[@"img"];
  161. [self.imageSource addObject:pubImageUrl];
  162. [self.dataSource addObject:image];
  163. [self.photoListView reloadData];
  164. } else {
  165. [MBProgressHUD showInfo:@"请求失败!"];
  166. }
  167. } enError:^(NSError *error) {
  168. [MBProgressHUD hideHUDForView:self.view animated:YES];
  169. }];
  170. }
  171. - (NSMutableArray *)dataSource {
  172. if (!_dataSource) {
  173. _dataSource = [NSMutableArray array];
  174. }
  175. return _dataSource;
  176. }
  177. - (void)didReceiveMemoryWarning {
  178. [super didReceiveMemoryWarning];
  179. // Dispose of any resources that can be recreated.
  180. }
  181. @end