SocialTopTableViewCell.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // SocialTopTableViewCell.m
  3. // NIMDemo
  4. //
  5. // Created by Fenix Wang on 2017/7/5.
  6. // Copyright © 2017年 Netease. All rights reserved.
  7. //
  8. #import "SocialTopTableViewCell.h"
  9. #import "NIMAvatarImageView.h"
  10. #import "UIView+NTES.h"
  11. #import "UIImageView+WebCache.h"
  12. #import "User.h"
  13. #import "HttpRequest.h"
  14. #import "UIActionSheet+NTESBlock.h"
  15. #import "SVProgressHUD.h"
  16. @interface SocialTopTableViewCell()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
  17. @property (nonatomic, strong) UIImageView *imgBg;
  18. @property (nonatomic, strong) UILabel *imgLabel;
  19. @property (nonatomic, strong) NIMAvatarImageView *avatarImg;
  20. @property (nonatomic, strong) UILabel *nameLabel;
  21. @property (nonatomic, strong) UserInfo *userInfo;
  22. @end
  23. @implementation SocialTopTableViewCell
  24. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  25. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  26. if(self)
  27. {
  28. [self setSelectionStyle:UITableViewCellSelectionStyleNone];
  29. }
  30. return self;
  31. }
  32. - (void)initSize:(CGFloat)cellWidth cellHeight:(CGFloat)cellHeight{
  33. _imgBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cellWidth, cellHeight)];
  34. _imgBg.userInteractionEnabled = YES;
  35. _imgBg.contentMode = UIViewContentModeScaleAspectFill;
  36. _imgBg.clipsToBounds = YES;
  37. [_imgBg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchBanner:)]];
  38. [self addSubview:_imgBg];
  39. _imgLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cellWidth, 20)];
  40. _imgLabel.text = @"轻触换背景";
  41. _imgLabel.centerY = _imgBg.centerY;
  42. _imgLabel.font = [UIFont systemFontOfSize:15];
  43. _imgLabel.textColor = UIColor.whiteColor;
  44. _imgLabel.shadowColor = UIColor.grayColor;
  45. _imgLabel.shadowOffset = CGSizeMake(1, 1);
  46. _imgLabel.textAlignment = NSTextAlignmentCenter;
  47. [self addSubview:_imgLabel];
  48. _avatarImg = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(15, cellHeight - 70, 60, 60)];
  49. [_avatarImg nim_setImageWithURL:nil placeholderImage:[UIImage imageNamed:@"avatar_user2"]];
  50. [_avatarImg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchAvatar:)]];
  51. [self addSubview:_avatarImg];
  52. _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_avatarImg.right+10, _avatarImg.centerY-15, 200, 30)];
  53. _nameLabel.textColor = UIColor.whiteColor;
  54. [self addSubview:_nameLabel];
  55. }
  56. - (void)setData:(UserInfo *)userInfo{
  57. _userInfo = userInfo;
  58. NIMUser *nimUser = [[NIMSDK sharedSDK].userManager userInfo:_userInfo.userId];
  59. NSURL *url = nimUser && nimUser.userInfo.avatarUrl ? [NSURL URLWithString:nimUser.userInfo.avatarUrl] : nil;
  60. [_avatarImg nim_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"avatar_user2"]];
  61. _nameLabel.text = [[User sharedInfo] getUserName:nimUser];
  62. if([userInfo.userId isEqualToString:[NIMSDK sharedSDK].loginManager.currentAccount])
  63. {
  64. _imgLabel.hidden = NO;
  65. }
  66. else
  67. {
  68. _imgLabel.hidden = YES;
  69. }
  70. [self updateBanner];
  71. }
  72. - (void)onTouchBanner:(UITapGestureRecognizer *)gestureRecognizer{
  73. if(![_userInfo.userId isEqualToString:[NIMSDK sharedSDK].loginManager.currentAccount])
  74. return;
  75. UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"设置封面" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册", nil];
  76. [sheet showInView:self.viewController.view completionHandler:^(NSInteger index) {
  77. switch (index) {
  78. case 0:
  79. [self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
  80. break;
  81. default:
  82. break;
  83. }
  84. }];
  85. }
  86. - (void)showImagePicker:(UIImagePickerControllerSourceType)type{
  87. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  88. picker.delegate = self;
  89. picker.sourceType = type;
  90. picker.allowsEditing = YES;
  91. [self.viewController.navigationController presentViewController:picker animated:YES completion:nil];
  92. }
  93. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  94. UIImage *image = info[UIImagePickerControllerEditedImage];
  95. [picker dismissViewControllerAnimated:YES completion:^{
  96. [self uploadImage:image];
  97. }];
  98. }
  99. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  100. [picker dismissViewControllerAnimated:YES completion:nil];
  101. }
  102. - (void)uploadImage:(UIImage *)image{
  103. __weak typeof(self) wself = self;
  104. [SVProgressHUD show];
  105. [[HttpRequest shared] uploadSocialBanner:image success:^(NSString * _Nullable imgURL) {
  106. [User sharedInfo].userInfo.socialBanner = imgURL;
  107. [wself updateBanner];
  108. } failure:^{
  109. [SVProgressHUD dismiss];
  110. }];
  111. }
  112. - (void)updateBanner{
  113. NSString *imgURL = _userInfo.socialBanner;
  114. NSURL *url = imgURL && imgURL.length > 0 ? [NSURL URLWithString:imgURL] : nil;
  115. [self.imgBg sd_setImageWithURL:url placeholderImage:User.defaultSocialBanner completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  116. [SVProgressHUD dismiss];
  117. }];
  118. }
  119. - (void)onTouchAvatar:(id)sender{
  120. [User showUserInfo:_userInfo.userId viewController:self.viewController];
  121. }
  122. @end