| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- //
- // SocialTopTableViewCell.m
- // NIMDemo
- //
- // Created by Fenix Wang on 2017/7/5.
- // Copyright © 2017年 Netease. All rights reserved.
- //
- #import "SocialTopTableViewCell.h"
- #import "NIMAvatarImageView.h"
- #import "UIView+NTES.h"
- #import "UIImageView+WebCache.h"
- #import "User.h"
- #import "HttpRequest.h"
- #import "UIActionSheet+NTESBlock.h"
- #import "SVProgressHUD.h"
- @interface SocialTopTableViewCell()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
- @property (nonatomic, strong) UIImageView *imgBg;
- @property (nonatomic, strong) UILabel *imgLabel;
- @property (nonatomic, strong) NIMAvatarImageView *avatarImg;
- @property (nonatomic, strong) UILabel *nameLabel;
- @property (nonatomic, strong) UserInfo *userInfo;
- @end
- @implementation SocialTopTableViewCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if(self)
- {
- [self setSelectionStyle:UITableViewCellSelectionStyleNone];
- }
- return self;
- }
- - (void)initSize:(CGFloat)cellWidth cellHeight:(CGFloat)cellHeight{
-
- _imgBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cellWidth, cellHeight)];
- _imgBg.userInteractionEnabled = YES;
- _imgBg.contentMode = UIViewContentModeScaleAspectFill;
- _imgBg.clipsToBounds = YES;
- [_imgBg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchBanner:)]];
- [self addSubview:_imgBg];
-
- _imgLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cellWidth, 20)];
- _imgLabel.text = @"轻触换背景";
- _imgLabel.centerY = _imgBg.centerY;
- _imgLabel.font = [UIFont systemFontOfSize:15];
- _imgLabel.textColor = UIColor.whiteColor;
- _imgLabel.shadowColor = UIColor.grayColor;
- _imgLabel.shadowOffset = CGSizeMake(1, 1);
- _imgLabel.textAlignment = NSTextAlignmentCenter;
- [self addSubview:_imgLabel];
-
-
- _avatarImg = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(15, cellHeight - 70, 60, 60)];
- [_avatarImg nim_setImageWithURL:nil placeholderImage:[UIImage imageNamed:@"avatar_user2"]];
- [_avatarImg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchAvatar:)]];
- [self addSubview:_avatarImg];
-
- _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_avatarImg.right+10, _avatarImg.centerY-15, 200, 30)];
- _nameLabel.textColor = UIColor.whiteColor;
- [self addSubview:_nameLabel];
- }
- - (void)setData:(UserInfo *)userInfo{
- _userInfo = userInfo;
- NIMUser *nimUser = [[NIMSDK sharedSDK].userManager userInfo:_userInfo.userId];
- NSURL *url = nimUser && nimUser.userInfo.avatarUrl ? [NSURL URLWithString:nimUser.userInfo.avatarUrl] : nil;
- [_avatarImg nim_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"avatar_user2"]];
-
- _nameLabel.text = [[User sharedInfo] getUserName:nimUser];
-
- if([userInfo.userId isEqualToString:[NIMSDK sharedSDK].loginManager.currentAccount])
- {
- _imgLabel.hidden = NO;
- }
- else
- {
- _imgLabel.hidden = YES;
- }
-
- [self updateBanner];
- }
- - (void)onTouchBanner:(UITapGestureRecognizer *)gestureRecognizer{
-
- if(![_userInfo.userId isEqualToString:[NIMSDK sharedSDK].loginManager.currentAccount])
- return;
-
- UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"设置封面" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册", nil];
- [sheet showInView:self.viewController.view completionHandler:^(NSInteger index) {
- switch (index) {
- case 0:
- [self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
- break;
- default:
- break;
- }
- }];
- }
- - (void)showImagePicker:(UIImagePickerControllerSourceType)type{
- UIImagePickerController *picker = [[UIImagePickerController alloc] init];
- picker.delegate = self;
- picker.sourceType = type;
- picker.allowsEditing = YES;
- [self.viewController.navigationController presentViewController:picker animated:YES completion:nil];
- }
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
- UIImage *image = info[UIImagePickerControllerEditedImage];
- [picker dismissViewControllerAnimated:YES completion:^{
- [self uploadImage:image];
- }];
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
- - (void)uploadImage:(UIImage *)image{
-
- __weak typeof(self) wself = self;
- [SVProgressHUD show];
- [[HttpRequest shared] uploadSocialBanner:image success:^(NSString * _Nullable imgURL) {
-
- [User sharedInfo].userInfo.socialBanner = imgURL;
-
- [wself updateBanner];
-
- } failure:^{
- [SVProgressHUD dismiss];
- }];
-
- }
- - (void)updateBanner{
- NSString *imgURL = _userInfo.socialBanner;
- NSURL *url = imgURL && imgURL.length > 0 ? [NSURL URLWithString:imgURL] : nil;
- [self.imgBg sd_setImageWithURL:url placeholderImage:User.defaultSocialBanner completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
- [SVProgressHUD dismiss];
- }];
- }
- - (void)onTouchAvatar:(id)sender{
- [User showUserInfo:_userInfo.userId viewController:self.viewController];
- }
- @end
|