UIImageView中的圆角

时间:2011-05-29 23:49:35

标签: iphone objective-c uiimageview

我有以下代码:

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

它确实给我一个围绕UIImage的圆形白色边框,但是在4个角落周围有额外的尖端..有没有办法将其剪掉?

enter image description here

2 个答案:

答案 0 :(得分:7)

setMasksToBounds可能就是你要找的东西。

[avatar.layer setMasksToBounds:YES];

答案 1 :(得分: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;
}