iOS更换头像

2018-12-04  本文已影响0人  盖世英雄的梦想

首先先导入SelectPhotoManager(换头像)这个
然后在viewcontroller导入SelectPhotoManager头文件

不要忘了改plist文件


屏幕快照 2018-12-04 上午11.29.15.png

添加Privacy - Photo Library Usage Description和Privacy - Camera Usage Description

@interface ViewController ()
@property (nonatomic, strong)SelectPhotoManager *photoManager;
@property(nonatomic,strong)UIImageView *imgview;

@end

@implementation ViewController

_imgview = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 80, 80)];
_imgview.image = [UIImage imageNamed:@"1"];
//圆角
[_imgview.layer setMasksToBounds:YES];
[_imgview.layer setCornerRadius:40.0];
[self.view addSubview:_imgview];





[self setHead];

}
-(void)tapClick:(UITapGestureRecognizer *)recognizer{

if (!_photoManager) {
    _photoManager =[[SelectPhotoManager alloc]init];
}
[_photoManager startSelectPhotoWithImageName:@"选择头像"];
__weak typeof(self)mySelf=self;
//选取照片成功
_photoManager.successHandle=^(SelectPhotoManager *manager,UIImage *image){
    
    mySelf.imgview.image = image;
    //保存到本地
    NSData *data = UIImagePNGRepresentation(image);
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"headerImage"];
};

}
-(void)setHead{
_imgview.userInteractionEnabled=YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
[_imgview addGestureRecognizer:tap];
//这里是从本地取的,如果是上线项目一定要从服务器取头像地址加载
UIImage *img = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"headerImage"]];
if (img) {
_imgview.image = img;
}

}

上一篇下一篇

猜你喜欢

热点阅读