导航栏添加头像 (网络获取图片地址)切圆

 

// 导航栏
- (void) navShowDiffentInfoWith:(NSString *)titleName
{
    UIBarButtonItem *button3= [[UIBarButtonItem alloc]initWithTitle:titleName style:UIBarButtonItemStyleDone  target:self action:@selector(addFollowAction)];
    NSURL *headerImageUrl = [NSURL URLWithStringAddOffset:self.homePageModelList.creator.headImg];
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    UIButton *button = [[UIButton alloc] initWithFrame:contentView.bounds];
    [button addTarget:self action:@selector(imageTapAction) forControlEvents:UIControlEventTouchUpInside];
    if (headerImageUrl) {
        // 网络头像
        [button setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:headerImageUrl]] forState:UIControlStateNormal];
    }else{
        // 本地头像
        [button setImage:[UIImage imageNamed:@"icon_detail_userphoto"] forState:UIControlStateNormal];
    }
    [contentView addSubview:button];
    contentView.layer.cornerRadius = 15;
    contentView.layer.masksToBounds = YES;
    
    UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithCustomView:contentView];
    self.navigationItem.rightBarButtonItems = @[button2,button3];
}

注:本地头像如果指定大小可以是圆的,网络图片地址一直会变大变形、这样写加载网络头像不管图片多大 都可以根据自己设置

contentView大小来调整,有问题可以留言、主页有我的联系方式
posted @ 2018-03-23 17:08  宁静暖风  阅读(366)  评论(0编辑  收藏  举报