adit
adit

Reputation: 33674

rounded corner in UIImageView

I have the following code:

[avatar.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[avatar.layer setBorderWidth:2.0];
[avatar.layer setShadowOffset:CGSizeMake(-1.0, -1.0)];
[avatar.layer setCornerRadius:8];

It does give me a rounded white border surrounding the UIImage, however there is that extra tip around the 4 corners.. is there a way to cut it off?

enter image description here

Upvotes: 2

Views: 484

Answers (2)

Savas Adar
Savas Adar

Reputation: 4260

-(UIImage *)makeRoundedImage:(UIImage *) image 
                      radius: (float) radius;
{
  CALayer *imageLayer = [CALayer layer];
  imageLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
  imageLayer.contents = (id) image.CGImage;

  imageLayer.masksToBounds = YES;
  imageLayer.cornerRadius = radius;

  UIGraphicsBeginImageContext(image.size);
  [imageLayer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return roundedImage;
}

Upvotes: 0

Sascha
Sascha

Reputation: 5973

setMasksToBounds is probably what you are looking for.

[avatar.layer setMasksToBounds:YES];

Upvotes: 7

Related Questions