美文网首页
iOS更换头像

iOS更换头像

作者: 盖世英雄的梦想 | 来源:发表于2018-12-04 11:31 被阅读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

    • (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
    _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;
    }

    }

    相关文章

      网友评论

          本文标题:iOS更换头像

          本文链接:https://www.haomeiwen.com/subject/hyzpcqtx.html