6

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

iOSDev
  • 3,617
  • 10
  • 51
  • 91

2 Answers2

30

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

iccir
  • 5,078
  • 2
  • 22
  • 34
1

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:

Community
  • 1
  • 1
Damien
  • 2,421
  • 22
  • 38
  • hmm yes, this can be a way of doing...will try out. However can we select particular area and then apply scaling to it? by using some touch event? – iOSDev Jan 30 '12 at 02:24