开发者

Rounded Rect / Rounded corners for images in UITableView

I use this category and create images for my UITableView to all be the same size. Is there a way to have the images have rounded corners as well? Thanks!

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

Edit: I then get the image, and other object info to put it in an NSDictionary to get in the UITableView. I tried changing the UIImageView.layer property in the cellForRowAtIndexPath, but it doesn't seem to do the trick:

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


You can add clipping to the drawing operation, the UIBezierPath class makes this super easy.

Extend you code to:

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


Try this

image.layer.cornerRadius = 5;


  1. Include QuartzCore framework.
  2. Import CALayer.h
  3. image.layer.cornerFRadius = 5;


As said Sisu and the.evangelist : image.layer.cornerRadius = 5;

But you may need to also add :

[image.layer setMasksToBounds:YES];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜