2

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

Suresh Varma
  • 9,750
  • 1
  • 60
  • 91
adit
  • 32,574
  • 72
  • 229
  • 373

2 Answers2

7

setMasksToBounds is probably what you are looking for.

[avatar.layer setMasksToBounds:YES];
Sascha
  • 5,903
  • 3
  • 24
  • 20
  • While setMaskToBounds will clip the view, I am pretty sure, the shadow will not be shown either (because it is clipped as well). So you might need to work around that by putting the clipped view into another larger view to enable the shadow. – marcus May 30 '11 at 07:45
  • what's the inside for this? – LiangWang Jan 14 '13 at 09:20
0
-(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;
}
Savas Adar
  • 4,083
  • 3
  • 46
  • 54