1

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 Answers 4

2

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

4
  • did i mention the homeButtonImage is a uiimageview? also, i have homeButtonImage.layer.masksToBounds = YES.
    – Marty
    Commented Sep 1, 2010 at 18:43
  • That should be the way to fix it. Did you create the UIImageView in IB? If so, make sure it's set there.
    – MishieMoo
    Commented Sep 1, 2010 at 20:20
  • that won't fix it because "the uiimage doesn't fill the entire UIImageView". The UIImageView will draw itself with a rounded border, but the UIImage itself is not drawn near the view's border in that case.
    – filipe
    Commented Sep 1, 2010 at 21:01
  • Why does the image not fill the entire view? If it did, then it would have rounded corners. Changing the contentMode on the image view to scaleToFit, aspectFit or aspectFill should do the trick. Unless the point is to not have the image take up the entire view?
    – MishieMoo
    Commented Sep 1, 2010 at 21:04
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;
}
0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.