SettingTopTableViewCell.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // SettingTopTableViewCell.m
  3. // NIMDemo
  4. //
  5. // Created by Fenix Wang on 2017/7/3.
  6. // Copyright © 2017年 Netease. All rights reserved.
  7. //
  8. #import "SettingTopTableViewCell.h"
  9. #import "NIMAvatarImageView.h"
  10. #import "User.h"
  11. #import "NIMSDK/NIMSDK.h"
  12. #import "UIView+NTES.h"
  13. #import "UIImageView+WebCache.h"
  14. #import "UIActionSheet+NTESBlock.h"
  15. #import "HttpRequest.h"
  16. #import "AddPhotoViewController.h"
  17. @interface SettingTopTableViewCell()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
  18. @property (strong, nonatomic) UIImageView *topImgView;
  19. @property (strong, nonatomic) NIMAvatarImageView *avatarImg;
  20. @property (strong, nonatomic) UIImageView *genderImg;
  21. @property (strong, nonatomic) UILabel *userNameLabel;
  22. @property (strong, nonatomic) UILabel *userIdLabel;
  23. @property (strong, nonatomic) UIView *photoContainer;
  24. @property (strong, nonatomic) UIButton *photoAddBtn;
  25. @property (nonatomic, assign) float cellWidth;
  26. @end
  27. @implementation SettingTopTableViewCell
  28. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  29. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  30. if(self)
  31. {
  32. [self setSelectionStyle:UITableViewCellSelectionStyleNone];
  33. }
  34. return self;
  35. }
  36. - (void)initSize:(CGFloat)cellWidth cellHeight:(CGFloat)cellHeight{
  37. self.cellWidth = cellWidth;
  38. NSString *userId = [NIMSDK sharedSDK].loginManager.currentAccount;
  39. NIMUser *nimUser = [[NIMSDK sharedSDK].userManager userInfo:userId];
  40. _topImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cellWidth, 180)];
  41. _topImgView.userInteractionEnabled = YES;
  42. _topImgView.contentMode = UIViewContentModeScaleAspectFill;
  43. _topImgView.clipsToBounds = YES;
  44. [_topImgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchTopImage:)]];
  45. [self addSubview:_topImgView];
  46. _avatarImg = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
  47. _avatarImg.centerX = cellWidth / 2;
  48. _avatarImg.centerY = _topImgView.bottom;
  49. [_avatarImg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchAvatar:)]];
  50. [self addSubview:_avatarImg];
  51. _genderImg = [[UIImageView alloc] initWithFrame:CGRectMake(_avatarImg.right-20, _avatarImg.bottom-20, 20, 20)];
  52. if(nimUser.userInfo.gender == NIMUserGenderMale || nimUser.userInfo.gender == NIMUserGenderUnknown)
  53. {
  54. [_genderImg setImage:[UIImage imageNamed:@"我的_07 (3)"]];
  55. }
  56. else if(nimUser.userInfo.gender == NIMUserGenderFemale)
  57. {
  58. [_genderImg setImage:[UIImage imageNamed:@"我的_07 (4)"]];
  59. }
  60. [self addSubview:_genderImg];
  61. _userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _avatarImg.bottom + 10, cellWidth, 20)];
  62. _userNameLabel.text = nimUser.userInfo.nickName ? nimUser.userInfo.nickName : userId;
  63. _userNameLabel.textAlignment = NSTextAlignmentCenter;
  64. [_userNameLabel setTextColor:UIColor.blackColor];
  65. [self addSubview:_userNameLabel];
  66. _userIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _userNameLabel.bottom, cellWidth, 20)];
  67. _userIdLabel.text = [NSString stringWithFormat:@"ID:%@", userId];
  68. _userIdLabel.textAlignment = NSTextAlignmentCenter;
  69. _userIdLabel.font = [UIFont systemFontOfSize:15];
  70. [_userIdLabel setTextColor:UIColor.lightGrayColor];
  71. [self addSubview:_userIdLabel];
  72. [self fillPhotos];
  73. UIView *step = [[UIView alloc] initWithFrame:CGRectMake(0, cellHeight-0.5, cellWidth, 0.5)];
  74. step.backgroundColor = UIColor.lightGrayColor;
  75. [self addSubview:step];
  76. }
  77. - (void)fillPhotos
  78. {
  79. [self updateBanner];
  80. if(!_photoContainer)
  81. {
  82. _photoContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 300, _cellWidth, 0)];
  83. _photoContainer.backgroundColor = UIColor.groupTableViewBackgroundColor;
  84. [self addSubview:_photoContainer];
  85. }
  86. float space = 1;
  87. int columns = 4;
  88. float picWidth = (_cellWidth - (columns - 1) * space) / columns;
  89. NSMutableArray *photos = [User sharedInfo].userInfo.photeArr;
  90. for(int i=0; i<photos.count; i++)
  91. {
  92. float x = (i%columns) * (picWidth+space);
  93. float y = floorf(i/columns) * (picWidth+space);
  94. UIImageView *imgView = nil;
  95. if(i >= _photoContainer.subviews.count)
  96. {
  97. imgView = [[UIImageView alloc] init];
  98. [_photoContainer addSubview:imgView];
  99. }
  100. else
  101. {
  102. imgView = [_photoContainer.subviews objectAtIndex:i];
  103. }
  104. [imgView setHidden:NO];
  105. [imgView setFrame:CGRectMake(x, y, picWidth, picWidth)];
  106. NSString *thumb = [[photos objectAtIndex:i] objectForKey:@"thumb"];
  107. NSURL *url = [NSURL URLWithString:thumb];
  108. [imgView sd_setImageWithURL:url placeholderImage:User.defaultPlaceHolderImage];
  109. }
  110. int numPhotos = (int)(photos.count);
  111. for(int j=numPhotos; j<_photoContainer.subviews.count; j++)
  112. {
  113. [[_photoContainer.subviews objectAtIndex:j] setHidden:YES];
  114. }
  115. _photoContainer.height = floorf(numPhotos/columns) * (picWidth+space) + picWidth;
  116. if(!_photoAddBtn)
  117. {
  118. _photoAddBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  119. _photoAddBtn.frame = CGRectMake(0, 0, picWidth, picWidth);
  120. _photoAddBtn.backgroundColor = UIColor.whiteColor;
  121. [self addSubview:_photoAddBtn];
  122. UIImageView *addIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, picWidth/4, picWidth/4)];
  123. addIcon.centerX = picWidth / 2;
  124. addIcon.bottom = picWidth / 2;
  125. [addIcon setImage:[UIImage imageNamed:@"编辑照片_03"]];
  126. [_photoAddBtn addSubview:addIcon];
  127. UILabel *addLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, picWidth/2+10, picWidth, 30)];
  128. addLabel.text = @"添加照片";
  129. addLabel.textColor = User.greenColor;
  130. addLabel.textAlignment = NSTextAlignmentCenter;
  131. [_photoAddBtn addSubview:addLabel];
  132. [_photoAddBtn addTarget:self action:@selector(onTouchAddPhoto:) forControlEvents:UIControlEventTouchUpInside];
  133. }
  134. float x = (numPhotos%columns) * (picWidth+space);
  135. float y = floorf(numPhotos/columns) * (picWidth+space) + _photoContainer.top;
  136. _photoAddBtn.left = x;
  137. _photoAddBtn.top = y;
  138. }
  139. - (void)updateBanner{
  140. NSString *imgURL = [User sharedInfo].userInfo.socialBanner;
  141. NSURL *url = imgURL && imgURL.length > 0 ? [NSURL URLWithString:imgURL] : nil;
  142. [self.topImgView sd_setImageWithURL:url placeholderImage:User.defaultSocialBanner];
  143. NSString *userId = [NIMSDK sharedSDK].loginManager.currentAccount;
  144. NIMUser *nimUser = [[NIMSDK sharedSDK].userManager userInfo:userId];
  145. url = nimUser.userInfo.avatarUrl ? [NSURL URLWithString:nimUser.userInfo.avatarUrl] : nil;
  146. [_avatarImg nim_setImageWithURL:url placeholderImage:User.defaultUserAvatar];
  147. }
  148. - (void)onTouchTopImage:(id)sender{
  149. UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"设置封面" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册", nil];
  150. [sheet showInView:self.viewController.view completionHandler:^(NSInteger index) {
  151. switch (index) {
  152. case 0:
  153. [self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
  154. break;
  155. default:
  156. break;
  157. }
  158. }];
  159. }
  160. - (void)showImagePicker:(UIImagePickerControllerSourceType)type{
  161. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  162. picker.delegate = self;
  163. picker.sourceType = type;
  164. picker.allowsEditing = YES;
  165. [self.viewController.navigationController presentViewController:picker animated:YES completion:nil];
  166. }
  167. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  168. UIImage *image = info[UIImagePickerControllerEditedImage];
  169. [picker dismissViewControllerAnimated:YES completion:^{
  170. [self uploadImage:image];
  171. }];
  172. }
  173. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  174. [picker dismissViewControllerAnimated:YES completion:nil];
  175. }
  176. - (void)uploadImage:(UIImage *)image{
  177. __weak typeof(self) wself = self;
  178. [[HttpRequest shared] uploadSocialBanner:image success:^(NSString * _Nullable imgURL) {
  179. [User sharedInfo].userInfo.socialBanner = imgURL;
  180. [wself updateBanner];
  181. } failure:^{
  182. }];
  183. }
  184. - (void)onTouchAddPhoto:(id)sender{
  185. AddPhotoViewController *vc = [[AddPhotoViewController alloc] init];
  186. [self.viewController.navigationController pushViewController:vc animated:YES];
  187. }
  188. - (void)onTouchAvatar:(id)sender{
  189. [User showUserInfo:[NIMSDK sharedSDK].loginManager.currentAccount viewController:self.viewController];
  190. }
  191. @end