views:

183

answers:

3

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.

+2  A: 

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

MishieMoo
did i mention the homeButtonImage is a uiimageview? also, i have homeButtonImage.layer.masksToBounds = YES.
marty
That should be the way to fix it. Did you create the UIImageView in IB? If so, make sure it's set there.
MishieMoo
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
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
A: 

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.

filipe
A: 

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

Brian