开发者

How to make rounded corners on image in uiimageview?

I have a uiimage in a uiimageview. the uiimage doesn't fill the entire uiimageview. I'm trying to have a border with rounded corners, but just around the uiimage, not the entire uiimageview. Can't figure out how to get it. I have

homeButtonImage.layer.cornerRadius = 5;
homeButtonImage.layer.m开发者_开发技巧asksToBounds = YES;
homeButtonImage.layer.borderColor = [UIColor blackColor].CGColor;
homeButtonImage.layer.borderWidth = 3.0;

but that draws the border on the entire uiimageview, not just the uiimage. help?

EDIT: nvm figured it out. I had it as an IBOutlet so that was messing it up.


You need to add [homeButtonImage setMasksToBounds:YES] otherwise it won't reflect the changes.


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


I believe the easiest way is to have your UIImageView object match the size of the UIImage. If you need to have that image inside a fixed sized view, put that UIImageView inside a UIView (or another UIImageView).

Or actually the easiest way to do it would be to simply make the rounded borders in your image file, but I'm assuming that is not an option for you.

You could also take the complicated path and subclass UIView and override drawRect, clipping the context around the actual UIImage and drawing it with CGContextDrawImage(ctx, rect, image.CGImage), but I would think that's overkill.


Call sizeToFit on the imageview. That should make it resize itself to match the size of the image.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜