How to cut a large sprite png into smaller UIImages?

后端 未结 2 649
-上瘾入骨i
-上瘾入骨i 2021-02-15 04:09

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

2条回答
  •  名媛妹妹
    2021-02-15 04:33

    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);
    

提交回复
热议问题