123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- //
- // SettingTopTableViewCell.m
- // NIMDemo
- //
- // Created by Fenix Wang on 2017/7/3.
- // Copyright © 2017年 Netease. All rights reserved.
- //
- #import "SettingTopTableViewCell.h"
- #import "NIMAvatarImageView.h"
- #import "User.h"
- #import "NIMSDK/NIMSDK.h"
- #import "UIView+NTES.h"
- #import "UIImageView+WebCache.h"
- #import "UIActionSheet+NTESBlock.h"
- #import "HttpRequest.h"
- #import "AddPhotoViewController.h"
- @interface SettingTopTableViewCell()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
- @property (strong, nonatomic) UIImageView *topImgView;
- @property (strong, nonatomic) NIMAvatarImageView *avatarImg;
- @property (strong, nonatomic) UIImageView *genderImg;
- @property (strong, nonatomic) UILabel *userNameLabel;
- @property (strong, nonatomic) UILabel *userIdLabel;
- @property (strong, nonatomic) UIView *photoContainer;
- @property (strong, nonatomic) UIButton *photoAddBtn;
- @property (nonatomic, assign) float cellWidth;
- @end
- @implementation SettingTopTableViewCell
- - (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{
-
- self.cellWidth = cellWidth;
-
- NSString *userId = [NIMSDK sharedSDK].loginManager.currentAccount;
- NIMUser *nimUser = [[NIMSDK sharedSDK].userManager userInfo:userId];
-
- _topImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cellWidth, 180)];
- _topImgView.userInteractionEnabled = YES;
- _topImgView.contentMode = UIViewContentModeScaleAspectFill;
- _topImgView.clipsToBounds = YES;
- [_topImgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchTopImage:)]];
- [self addSubview:_topImgView];
-
- _avatarImg = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
- _avatarImg.centerX = cellWidth / 2;
- _avatarImg.centerY = _topImgView.bottom;
- [_avatarImg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchAvatar:)]];
-
-
- [self addSubview:_avatarImg];
-
- _genderImg = [[UIImageView alloc] initWithFrame:CGRectMake(_avatarImg.right-20, _avatarImg.bottom-20, 20, 20)];
- if(nimUser.userInfo.gender == NIMUserGenderMale || nimUser.userInfo.gender == NIMUserGenderUnknown)
- {
- [_genderImg setImage:[UIImage imageNamed:@"我的_07 (3)"]];
- }
- else if(nimUser.userInfo.gender == NIMUserGenderFemale)
- {
- [_genderImg setImage:[UIImage imageNamed:@"我的_07 (4)"]];
- }
- [self addSubview:_genderImg];
-
- _userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _avatarImg.bottom + 10, cellWidth, 20)];
- _userNameLabel.text = nimUser.userInfo.nickName ? nimUser.userInfo.nickName : userId;
- _userNameLabel.textAlignment = NSTextAlignmentCenter;
- [_userNameLabel setTextColor:UIColor.blackColor];
- [self addSubview:_userNameLabel];
-
- _userIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _userNameLabel.bottom, cellWidth, 20)];
- _userIdLabel.text = [NSString stringWithFormat:@"ID:%@", userId];
- _userIdLabel.textAlignment = NSTextAlignmentCenter;
- _userIdLabel.font = [UIFont systemFontOfSize:15];
- [_userIdLabel setTextColor:UIColor.lightGrayColor];
- [self addSubview:_userIdLabel];
-
-
-
- [self fillPhotos];
-
-
-
- UIView *step = [[UIView alloc] initWithFrame:CGRectMake(0, cellHeight-0.5, cellWidth, 0.5)];
- step.backgroundColor = UIColor.lightGrayColor;
- [self addSubview:step];
- }
- - (void)fillPhotos
- {
- [self updateBanner];
-
- if(!_photoContainer)
- {
- _photoContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 300, _cellWidth, 0)];
- _photoContainer.backgroundColor = UIColor.groupTableViewBackgroundColor;
- [self addSubview:_photoContainer];
- }
-
- float space = 1;
- int columns = 4;
- float picWidth = (_cellWidth - (columns - 1) * space) / columns;
-
- NSMutableArray *photos = [User sharedInfo].userInfo.photeArr;
- for(int i=0; i<photos.count; i++)
- {
- float x = (i%columns) * (picWidth+space);
- float y = floorf(i/columns) * (picWidth+space);
- UIImageView *imgView = nil;
- if(i >= _photoContainer.subviews.count)
- {
- imgView = [[UIImageView alloc] init];
- [_photoContainer addSubview:imgView];
- }
- else
- {
- imgView = [_photoContainer.subviews objectAtIndex:i];
- }
-
- [imgView setHidden:NO];
- [imgView setFrame:CGRectMake(x, y, picWidth, picWidth)];
-
- NSString *thumb = [[photos objectAtIndex:i] objectForKey:@"thumb"];
- NSURL *url = [NSURL URLWithString:thumb];
- [imgView sd_setImageWithURL:url placeholderImage:User.defaultPlaceHolderImage];
- }
-
- int numPhotos = (int)(photos.count);
- for(int j=numPhotos; j<_photoContainer.subviews.count; j++)
- {
- [[_photoContainer.subviews objectAtIndex:j] setHidden:YES];
- }
-
- _photoContainer.height = floorf(numPhotos/columns) * (picWidth+space) + picWidth;
-
-
- if(!_photoAddBtn)
- {
- _photoAddBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _photoAddBtn.frame = CGRectMake(0, 0, picWidth, picWidth);
- _photoAddBtn.backgroundColor = UIColor.whiteColor;
- [self addSubview:_photoAddBtn];
-
-
- UIImageView *addIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, picWidth/4, picWidth/4)];
- addIcon.centerX = picWidth / 2;
- addIcon.bottom = picWidth / 2;
- [addIcon setImage:[UIImage imageNamed:@"编辑照片_03"]];
- [_photoAddBtn addSubview:addIcon];
-
- UILabel *addLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, picWidth/2+10, picWidth, 30)];
- addLabel.text = @"添加照片";
- addLabel.textColor = User.greenColor;
- addLabel.textAlignment = NSTextAlignmentCenter;
- [_photoAddBtn addSubview:addLabel];
-
- [_photoAddBtn addTarget:self action:@selector(onTouchAddPhoto:) forControlEvents:UIControlEventTouchUpInside];
- }
-
- float x = (numPhotos%columns) * (picWidth+space);
- float y = floorf(numPhotos/columns) * (picWidth+space) + _photoContainer.top;
- _photoAddBtn.left = x;
- _photoAddBtn.top = y;
- }
- - (void)updateBanner{
- NSString *imgURL = [User sharedInfo].userInfo.socialBanner;
- NSURL *url = imgURL && imgURL.length > 0 ? [NSURL URLWithString:imgURL] : nil;
- [self.topImgView sd_setImageWithURL:url placeholderImage:User.defaultSocialBanner];
-
- NSString *userId = [NIMSDK sharedSDK].loginManager.currentAccount;
- NIMUser *nimUser = [[NIMSDK sharedSDK].userManager userInfo:userId];
-
- url = nimUser.userInfo.avatarUrl ? [NSURL URLWithString:nimUser.userInfo.avatarUrl] : nil;
- [_avatarImg nim_setImageWithURL:url placeholderImage:User.defaultUserAvatar];
- }
- - (void)onTouchTopImage:(id)sender{
-
- 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;
- [[HttpRequest shared] uploadSocialBanner:image success:^(NSString * _Nullable imgURL) {
-
- [User sharedInfo].userInfo.socialBanner = imgURL;
-
- [wself updateBanner];
-
- } failure:^{
-
- }];
-
- }
- - (void)onTouchAddPhoto:(id)sender{
-
- AddPhotoViewController *vc = [[AddPhotoViewController alloc] init];
- [self.viewController.navigationController pushViewController:vc animated:YES];
-
- }
- - (void)onTouchAvatar:(id)sender{
- [User showUserInfo:[NIMSDK sharedSDK].loginManager.currentAccount viewController:self.viewController];
- }
- @end
|