1

i need to crop single image into 9 pieces.

for that most of them suggest the fallowing method.

 - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

But i did n't fine any where how do it using this.

can any one please help me.

how can i crop image using this method.

Thank u in advance.

Mahesh Babu
  • 3,395
  • 9
  • 48
  • 97
  • Dude ... you already asked this question http://stackoverflow.com/questions/4743242/how-to-crop-image-in-to-pieces-programmatically/4744599#4744599 (by the way, the answer I gave in your previous question is correct) – fsaint Jan 22 '11 at 09:52

1 Answers1

3

The method stretchableImageWithLeftCapWidth:topCapHeight is not cropping the image. It allows you to stretch an image with vertical and horyzontal margin.

In the doc you will read :

stretchableImageWithLeftCapWidth:topCapHeight: Creates and returns a new image object with the specified cap values.

To crop an image you should use CGImageCreateWithImageInRect :

CGImageCreateWithImageInRect Creates a bitmap image using the data contained within a subregion of an existing bitmap image.

You can achieve what you want with a method such as this one :

- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {

    CGImageRef sourceImageRef = [image CGImage];  
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);  
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:1.0 orientation:image.imageOrientation];
    CGImageRelease(newImageRef);
    return newImage;
} 

Hope this helps, Vincent

vdaubry
  • 11,369
  • 7
  • 54
  • 76