Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 847
iOSDev Avatar asked Jan 30 '12 01:01

iOSDev


2 Answers

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

like image 94
iccir Avatar answered Oct 06 '22 00:10

iccir


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:

  • Cropping a UIImage
  • CGImage Reference
  • Quartz 2D Programming Guide
like image 32
Damien Avatar answered Oct 06 '22 00:10

Damien