iOSDev
iOSDev

Reputation: 3617

How to scale only specific parts of image in iPhone app?

I want to scale the image in iPhone app but not entirely. I just want to scale specific parts like the bottom part or the middle part...How do I do it?

Please help.

Thanks

Upvotes: 6

Views: 6733

Answers (2)

iccir
iccir

Reputation: 5128

It sounds like you want to do a form of 9-slice scaling or 3-slice scaling. Let's say you have the following image:

Original

and you want to make it look like this:

Scaled

(the diagonal end pieces do not stretch at all, the top and bottom pieces stretch horizontal, and the left and right pieces stretch vertical)

To do this, use -stretchableImageWithLeftCapWidth:topCapHeight: in iOS 4.x and earlier, or -resizableImageWithCapInsets: starting with iOS 5.

UIImage *myImage = [UIImage imageNamed:@"FancyButton"];
UIImage *myResizableImage = [myImage resizableImageWithCapInsets:UIEdgeInsetsMake(21.0, 13.0, 21.0, 13.0)];
[anImageView setImage:myResizableImage]

To help visualize the scaling, here is an image showing the above cap insets:

Insets

Upvotes: 30

Damien
Damien

Reputation: 2421

I'm not aware of any way to adjust the scale of just a part of a UIImage. I'd approach is slightly differently by creating seperate images from your primary image using CGImageCreateWithImageInRect and then scaling the seperate images with the different rates that you require.

See:

Upvotes: 1

Related Questions