我使用此类别并为我的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;
答案 0 :(得分:7)
您可以向绘图操作添加剪裁,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;
}
答案 1 :(得分:1)
试试这个
image.layer.cornerRadius = 5;
答案 2 :(得分:1)
答案 3 :(得分:0)
正如Sisu和the.evangelist所说:image.layer.cornerRadius = 5;
但您可能还需要添加:
[image.layer setMasksToBounds:YES];