JLSingleFaceViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // JLSingleFaceViewController.m
  3. // shiku_im
  4. //
  5. // Created by JayLuo on 2019/12/12.
  6. // Copyright © 2019 Reese. All rights reserved.
  7. //
  8. #import "JLSingleFaceViewController.h"
  9. #import "JLFacePackgeViewCell.h"
  10. #import "JLFacePackgeViewHeader.h"
  11. #import "JLFacePackgeModel.h"
  12. #import "JLFacePackgeDetailViewController.h"
  13. #import "JLRecommendFacePackgeViewController.h"
  14. #import "RITLPhotos.h"
  15. #import "ImageBrowserViewController.h"
  16. #define cellID @"JLFacePackgeViewCell"
  17. @interface JLSingleFaceViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,RITLPhotosViewControllerDelegate>
  18. @property (nonatomic, strong) UICollectionView *collectionView;
  19. @property (nonatomic, strong) NSMutableArray *facePackages;
  20. @property (nonatomic, weak) UILabel *label;
  21. @property (nonatomic, assign) BOOL isEdit;
  22. @property (nonatomic, strong) JLFacePackgeModel *model;
  23. @property (nonatomic, strong) NSMutableString *ids;
  24. @property (nonatomic, strong) NSArray *imgDataArr;
  25. @end
  26. @implementation JLSingleFaceViewController
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. // Do any additional setup after loading the view.
  30. self.isGotoBack = YES;
  31. self.title = @"单个表情 (0/300)";
  32. self.heightFooter = 0;
  33. self.heightHeader = JX_SCREEN_TOP;
  34. [self createHeadAndFoot];
  35. _facePackages = [NSMutableArray array];
  36. _isEdit = 1;
  37. _ids = [NSMutableString stringWithString:@""];
  38. // 1 单个收藏表情
  39. [g_server faceClollectListType:@"1" View:self];
  40. [self setupUI];
  41. }
  42. - (void)setupUI {
  43. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
  44. label.text = @"编辑";
  45. UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:label];
  46. [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightButtonClick)]];
  47. label.userInteractionEnabled = YES;
  48. [self setRightBarButtonItem:item];
  49. _label = label;
  50. _model = [[JLFacePackgeModel alloc] init];
  51. // _model.path = @"addfriend_room";
  52. _model.id = @"plusButtonID";
  53. [self collectionView];
  54. }
  55. // 点击加号
  56. - (void)dealwithPlus {
  57. RITLPhotosViewController *photoController = RITLPhotosViewController.photosViewController;
  58. photoController.configuration.maxCount = 9;//最大的选择数目
  59. photoController.configuration.containVideo = YES;//选择类型,目前只选择图片不选择视频
  60. photoController.photo_delegate = self;
  61. photoController.thumbnailSize = CGSizeMake(320, 320);//缩略图的尺寸
  62. [self presentViewController:photoController animated:true completion:^{}];
  63. }
  64. - (void)photosViewController:(UIViewController *)viewController assets:(NSArray <PHAsset *> *)assets {
  65. self.imgDataArr = assets;
  66. }
  67. #pragma mark - 发送图片
  68. - (void)photosViewController:(UIViewController *)viewController datas:(NSArray <id> *)datas; {
  69. for (int i = 0; i < datas.count; i++) {
  70. BOOL isGif = [datas[i] isKindOfClass:[NSData class]];
  71. if (isGif) {
  72. // GIF
  73. NSString *file = [FileInfo getUUIDFileName:@"gif"];
  74. [g_server saveDataToFile:datas[i] file:file];
  75. [self sendImage:file withWidth:0 andHeight:0 userId:nil];
  76. }else {
  77. // 普通图片
  78. UIImage *chosedImage = datas[i];
  79. //获取image的长宽
  80. int imageWidth = chosedImage.size.width;
  81. int imageHeight = chosedImage.size.height;
  82. NSString *name = @"jpg";
  83. NSString *file = [FileInfo getUUIDFileName:name];
  84. [g_server saveImageToFile:chosedImage file:file isOriginal:YES];
  85. [self sendImage:file withWidth:imageWidth andHeight:imageHeight userId:nil];
  86. }
  87. }
  88. }
  89. //图片piker选择完成后调用
  90. -(void)sendImage:(NSString *)file withWidth:(int) width andHeight:(int) height userId:(NSString *)userId
  91. {
  92. if ([file length]>0) {
  93. JXMessageObject *msg=[[JXMessageObject alloc]init];
  94. msg.timeSend = [NSDate date];
  95. msg.fromUserId = MY_USER_ID;
  96. msg.fileName = file;
  97. msg.content = [[file lastPathComponent] stringByDeletingPathExtension];
  98. msg.type = [NSNumber numberWithInt:kWCMessageTypeCustomFace];
  99. msg.isSend = [NSNumber numberWithInt:transfer_status_ing];
  100. msg.isRead = [NSNumber numberWithBool:NO];
  101. msg.isUpload = [NSNumber numberWithBool:NO];
  102. //新添加的图片宽高
  103. msg.location_x = [NSNumber numberWithInt:width];
  104. msg.location_y = [NSNumber numberWithInt:height];
  105. // 上传图片
  106. [g_server uploadFile:file validTime:@"300" messageId:nil toView:self];
  107. }
  108. }
  109. - (void)rightButtonClick {
  110. _isEdit = !_isEdit;
  111. _label.text = _isEdit?@"编辑":@"删除";
  112. if (_isEdit) {
  113. [_facePackages insertObject:_model atIndex:0];
  114. } else {
  115. [_facePackages removeObject:_model];
  116. }
  117. // 删除
  118. if (_isEdit) {
  119. // 通知为不选中状态
  120. [g_notify postNotificationName:@"NOTSELECTED" object:nil];
  121. NSString *ids = [NSString stringWithString:_ids];
  122. if ([ids isEqualToString:@""]) {
  123. [_collectionView reloadData];
  124. return;
  125. }
  126. ids = [ids substringWithRange:NSMakeRange(0, [ids length] - 1)];
  127. [g_server faceClollectDeleteFaceClollect:ids View:self];
  128. }
  129. [_collectionView reloadData];
  130. }
  131. - (void)didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  132. if([aDownload.action isEqualToString:act_FaceClollectList]){
  133. _facePackages = [JLFacePackgeModel mj_objectArrayWithKeyValuesArray:array1];
  134. self.title = [NSString stringWithFormat:@"单个表情 (%ld/300)", _facePackages.count];
  135. [_facePackages insertObject:_model atIndex:0];
  136. [self.collectionView reloadData];
  137. }
  138. if([aDownload.action isEqualToString:act_FaceClollectDeleteFaceClollect]){
  139. // 删除成功
  140. [SVProgressHUD setMinimumDismissTimeInterval:2.0];
  141. [SVProgressHUD showSuccessWithStatus:@"删除成功"];
  142. // 通知键盘
  143. [g_notify postNotificationName:kFavoritesRefresh object:nil];
  144. [g_server faceClollectListType:@"1" View:self];
  145. }
  146. if([aDownload.action isEqualToString:act_UploadFile]){
  147. // 上传图片成功
  148. NSString *faceName = dict[@"images"][0][@"oFileName"];
  149. NSString *url = dict[@"images"][0][@"oUrl"];
  150. // 添加单个表情
  151. [g_server addFaceClollect:@"" faceName:faceName url:url View:self];
  152. }
  153. if([aDownload.action isEqualToString:act_FaceClollectAddFaceClollect]){
  154. // 上传图片成功
  155. [SVProgressHUD setMinimumDismissTimeInterval:2.0];
  156. [SVProgressHUD showSuccessWithStatus:@"上传成功"];
  157. // 通知键盘
  158. [g_notify postNotificationName:kFavoritesRefresh object:nil];
  159. // 通知为不选中状态
  160. [g_notify postNotificationName:@"NOTSELECTED" object:nil];
  161. [g_server faceClollectListType:@"1" View:self];
  162. }
  163. }
  164. - (UICollectionView *)collectionView {
  165. if (_collectionView == nil) {
  166. UICollectionViewFlowLayout *layout = ({
  167. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  168. // 设置尺寸
  169. CGFloat width = self_width / 5;
  170. layout.itemSize = CGSizeMake(width, width);
  171. //估算的尺寸(一般不需要设置)
  172. // layout.estimatedItemSize = CGSizeMake(120, 130);
  173. //头部的参考尺寸(就是尺寸)
  174. // layout.headerReferenceSize = CGSizeMake(self_width, 50);
  175. //尾部的参考尺寸
  176. layout.footerReferenceSize = CGSizeMake(100, 100);
  177. layout.sectionFootersPinToVisibleBounds = YES;
  178. layout.sectionHeadersPinToVisibleBounds = YES;
  179. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  180. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  181. layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
  182. layout.minimumInteritemSpacing = 10;
  183. layout.minimumLineSpacing = 10;
  184. layout;
  185. });
  186. _collectionView = ({
  187. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  188. collectionView.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
  189. collectionView.backgroundColor = [UIColor clearColor];
  190. collectionView.frame = CGRectMake(0, JX_SCREEN_TOP, self.view.bounds.size.width, self.view.bounds.size.height-JX_SCREEN_TOP);
  191. collectionView.dataSource = self;
  192. collectionView.delegate = self;
  193. [collectionView registerNib:[UINib nibWithNibName:@"JLFacePackgeViewCell" bundle:nil] forCellWithReuseIdentifier:cellID];
  194. [collectionView registerNib:[UINib nibWithNibName:@"JLFacePackgeViewHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"JLFacePackgeViewHeaderID"];
  195. //设置滚动条
  196. collectionView.showsHorizontalScrollIndicator = NO;
  197. collectionView.showsVerticalScrollIndicator = YES;
  198. //设置是否需要弹簧效果
  199. collectionView.bounces = NO;
  200. collectionView;
  201. });
  202. [self.view addSubview:_collectionView];
  203. }
  204. return _collectionView;
  205. }
  206. #pragma mark - UICollectionViewDataSource
  207. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  208. return 1;
  209. }
  210. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  211. return _facePackages.count;
  212. }
  213. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  214. JLFacePackgeViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  215. cell.model = _facePackages[indexPath.row];
  216. cell.isSelectedImageHidden = !_isEdit;
  217. cell.JLFacePackgeViewCellCallBack = ^(NSString * _Nonnull idString, BOOL isSelected) {
  218. if ([idString isEqualToString:@"plusButtonID"]) {
  219. // 点击加号
  220. [self dealwithPlus];
  221. return ;
  222. }
  223. if (isSelected) {
  224. if ([_ids containsString:idString]) {
  225. return;
  226. }
  227. [_ids appendString:idString];
  228. [_ids appendString:@","];
  229. } else {
  230. if ([_ids containsString:idString]) {
  231. [_ids replaceOccurrencesOfString:[NSString stringWithFormat:@"%@,", idString] withString:@"" options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, [_ids length])];
  232. }
  233. }
  234. // NSLog(@"---------%@", _ids);
  235. };
  236. return cell;
  237. }
  238. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  239. return UIEdgeInsetsMake(5, 10, 5, 10);
  240. }
  241. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  242. UICollectionReusableView *headerView = nil;
  243. if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
  244. JLFacePackgeViewHeader *view = (JLFacePackgeViewHeader *) [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"JLFacePackgeViewHeaderID" forIndexPath:indexPath];
  245. headerView = view;
  246. }
  247. return headerView;
  248. }
  249. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  250. return CGSizeMake(self_width, 0.5);
  251. }
  252. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
  253. return CGSizeZero;
  254. }
  255. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  256. JLFacePackgeModel *model = _facePackages[indexPath.row];
  257. [ImageBrowserViewController show:self delegate:self isReadDel:YES type:PhotoBroswerVCTypeModal contentArray:@[model.url].mutableCopy index:0 imagesBlock:^NSArray *{
  258. return @[model.url];
  259. }];
  260. }
  261. @end