1
votes

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.masksToBounds = 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.

4

4 Answers

2
votes

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

0
votes

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.

0
votes

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

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