如何将大型精灵png切割成较小的UIImages? [英] How to cut a large sprite png into smaller UIImages?

查看:103
本文介绍了如何将大型精灵png切割成较小的UIImages?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,png文件是1200(h)x 50(w)像素,我如何剪切 png并加载6 UIImage s,每个200(h)x 50(w)。谢谢!

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!

编辑 - 感谢Michal的回答,最终代码:

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


推荐答案

查看CGImageCreateWithImageInRect函数。它适用于CGImage,但很容易在那个和UIImage之间进行转换。

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

这篇关于如何将大型精灵png切割成较小的UIImages?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆