如何将大精灵 png 切成较小的 UIImages?
例如,png文件是1200(h)x 50(w)像素,我如何剪切 png并在6个UIImage
中加载,每个200(h) x 50(宽)。谢谢!
编辑 - 感谢 Michal 的回答,最终代码:
CGImageRef imageToSplit = [UIImage imageNamed:@"huge.png"].CGImage;
CGImageRef partOfImageAsCG = CGImageCreateWithImageInRect(imageToSplit, CGRectMake(0, 0, 50, 50));
UIImage *partOfImage = [UIImage imageWithCGImage:partOfImageAsCG];
// ...
CGImageRelease(partOfImageAsCG);
For example, the png file is 1200 (h) x 50 (w) pixels, how can I cut the png and loads in 6 UIImage
s, 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看一下 CGImageCreateWithImageInRect 函数。它与 CGImage 一起使用,但在 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):
可重用的方法:
仅供参考:
尽管它已经得到了明确的回答,但我认为将其作为简单的可重用“复制粘贴”代码片段会给程序员带来很大的帮助,我将答案归因于 michal
A reusable method:
FYI:
Even though it has been answered clearly I thought giving it as simple reusable 'Copy-Paste' code snippet would be a lots of help to programmers and I attribute the answer to michal