iphone - 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 类使这变得 super 简单。

将您的代码扩展到:

+ (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;
}

https://stackoverflow.com/questions/6832892/

相关文章:

css - float : left; Not working in IE?

qt - 将插槽添加到自动创建的菜单项

qt - 如何解决(不兼容的发送方/接收方参数)问题?

asp.net - CS0433 : The type 'CrystalDecisions.Web.

sql-server - 用于生成序列号的 SQL Server 函数

java - 为什么我们不能将实例变量传递给父类(super class)构造函数?

php - 需要 return ($a > $b) 的解释吗? -1 : 1 in php

asp.net - 有什么简单的方法可以检查复选框列表控件中是否没有选择任何项目?

ruby-on-rails - bcrypt 解密器

jquery - 你如何使用 jquery 将一个下拉列表的选项复制到另一个下拉列表