Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cut a large sprite png into smaller UIImages?

Tags:

iphone

uiimage

For example, the png file is 1200 (h) x 50 (w) pixels, how can I cut the png and loads in 6 UIImages, each 200 (h) x 50 (w). Thanks!

EDIT - thanks to Michal's answer, the final code:

 CGImageRef imageToSplit = [UIImage imageNamed:@"huge.png"].CGImage;
 CGImageRef partOfImageAsCG = CGImageCreateWithImageInRect(imageToSplit, CGRectMake(0, 0, 50, 50));

 UIImage *partOfImage = [UIImage imageWithCGImage:partOfImageAsCG]; 
 // ...

 CGImageRelease(partOfImageAsCG);
like image 272
ohho Avatar asked Sep 22 '10 04:09

ohho


People also ask

How do I resize a sprite image?

You can resize your sprite using Edit > Sprite Size menu option.

How do you break apart a sprite sheet?

Click on Slice to open the window to slice (break up) the sprite sheet.

How do you slice a sprite sheet Aseprite?

( Shift+C key) you can indicate regions of your sprite and assign a name/label to that region with some extra user defined information. There is support to specify 9-slices/9-patches information.


1 Answers

Look at CGImageCreateWithImageInRect function. It works with CGImage, but it's easy to convert between that one and UIImage.

Here's an example (typed from memory, might not compile):

CGImageRef imageToSplit = [UIImage imageNamed:@"huge.png"].CGImage;
CGImageRef partOfImageAsCG = CGImageCreateWithImageInRect(imageToSplit, CGRectMake(0, 0, 200, 50));
CGRelease(imageToSplit);
UIImage *partOfImage = [UIImage imageWithCGImage:partOfImageAsCG];
CGImageRelease(partOfImageAsCG);
like image 75
Michal Avatar answered Oct 21 '22 08:10

Michal