开发者

rounded corner in UIImageView

开发者 https://www.devze.com 2023-03-09 07:17 出处:网络
I have the following code: [avatar.layer setBorderColor:[[UIColor whiteColor] CGColor]]; [avatar.layer setBorderWidth:2.0];

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?

rounded corner in UIImageView


setMasksToBounds is probably what you are looking for.

[avatar.layer setMasksToBounds:YES];


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

精彩评论

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

关注公众号