繁体   English   中英

UITableView 中图像的圆角矩形/圆角

[英]Rounded Rect / Rounded corners for images in UITableView

我使用这个类别并为我的 UITableView 创建图像,使其大小相同。 有没有办法让图像也有圆角? 谢谢!

+ (UIImage *)scale:(UIImage *)image toSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

编辑:然后我获取图像和其他对象信息以将其放入 NSDictionary 以进入 UITableView。 我尝试更改 cellForRowAtIndexPath 中的 UIImageView.layer 属性,但它似乎没有奏效:

cell.TitleLabel.text = [dict objectForKey:@"Name"];
cell.CardImage.image = [dict objectForKey:@"Image"];
cell.CardImage.layer.cornerRadius = 5.0;

您可以在绘图操作中添加剪辑, UIBezierPath类让这变得超级简单。

将您的代码扩展到:

+ (UIImage *)scale:(UIImage *)image toSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    [[UIBezierPath bezierPathWithRoundeRect:rect cornerRadius:5] addClip];
    [image drawInRect:rect];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

尝试这个

image.layer.cornerRadius = 5;
  1. 包括 QuartzCore 框架。
  2. 导入 CALayer.h
  3. image.layer.cornerFRadius = 5;

正如 Sisu 和 the.evangelist 所说: image.layer.cornerRadius = 5;

但您可能还需要添加:

[image.layer setMasksToBounds:YES];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM