UITableViewCell中设置不同图片尺寸相同以及设置图片为圆形的方法

本文介绍了如何在UITableViewCell中确保不同图片显示为相同尺寸,并提供了将图片设置为圆形的方法,类似于QQ界面的自定义cell实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、UITableViewCell中设置不同图片尺寸相同

//    图片尺寸不一样  修改cell左侧显示图片大小的方法  UIGraphics---

<span style="font-size:14px;font-family: KaiTi_GB2312; color: rgb(204, 153, 51);">   </span><span style="font-size:18px;color:#333333;font-family: SimSun;"> </span><span style="font-family:System;font-size:14px;color:#333333;">UIImage *icon = [UIImage imageNamed:[NSString stringWithFormat:@"%@", model.singerHeaderName]];;
    CGSize itemSize = CGSizeMake(70, 70);
    UIGraphicsBeginImageContext(itemSize);
    CGRect imageRect = CGRectMake(0, 0, itemSize.width, itemSize.height);
    [icon drawInRect:imageRect];
    
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();</span>


二、UITableViewCell中设置图片为圆形的方法
本人暂时没有找到其他的方法,现在用的自定义cell 来实现的,有朋友知道的话可以交流下

<span style="font-size:14px;">- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.iv = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 40, 40)];
        self.iv.backgroundColor = [UIColor clearColor];
        self.iv.layer.cornerRadius = CGRectGetWidth(self.iv.frame)/2;
        self.iv.layer.masksToBounds = YES;
        [self.contentView addSubview:self.iv];
        
        UIImage *image = [[UIImage alloc]init];
        self.iv.image = image;
        
        self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.iv.frame)+5, 5, CGRectGetWidth([UIScreen mainScreen].bounds)-CGRectGetMaxX(self.iv.frame)-5, 15)];
        self.nameLabel.backgroundColor = [UIColor clearColor];
        self.nameLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.nameLabel];
        
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.iv.frame)+5, 25, CGRectGetWidth([UIScreen mainScreen].bounds)-CGRectGetMaxX(self.iv.frame)-5, 15)];
        self.titleLabel.backgroundColor = [UIColor clearColor];
         self.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.titleLabel];
    }
    return self;
}</span>


示例图片:图一为设置不同尺寸的图片为相同的尺寸,图二为自定义cell,使得image为圆形,似QQ界面




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值