如何将大精灵 png 切成较小的 UIImages?

发布于 2024-09-24 20:57:03 字数 440 浏览 0 评论 0原文

例如,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 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生未歇 2024-10-01 20:57:03

看一下 CGImageCreateWithImageInRect 函数。它与 CGImage 一起使用,但在 CGImage 和 UIImage 之间进行转换很容易。

这是一个示例(凭记忆输入,可能无法编译):

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

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);
橙幽之幻 2024-10-01 20:57:03

可重用的方法:

-(UIImage*)ExtractImageOn:(CGPoint)pointExtractedImg ofSize:(CGSize)sizeExtractedImg FromSpriteSheet:(UIImage*)imgSpriteSheet
{
    UIImage *ExtractedImage;

    CGRect rectExtractedImage;

    rectExtractedImage=CGRectMake(pointExtractedImg.x,pointExtractedImg.y,sizeExtractedImg.width,sizeExtractedImg.height);

    CGImageRef imgRefSpriteSheet=imgSpriteSheet.CGImage;

    CGImageRef imgRefExtracted=CGImageCreateWithImageInRect(imgRefSpriteSheet,rectExtractedImage);

    ExtractedImage=[UIImage imageWithCGImage:imgRefExtracted];

    CGImageRelease(imgRefExtracted);

    //CGImageRelease(imgRefSpriteSheet); I have commented it because
    // we should not release the object that we don't own.
    // So why do we release imgRefExtracted alone? because it has 
    // name create in its method so the ownership comes to us so we
    // have to release it.

    return ExtractedImage;

}

仅供参考:

尽管它已经得到了明确的回答,但我认为将其作为简单的可重用“复制粘贴”代码片段会给程序员带来很大的帮助,我将答案归因于 michal

A reusable method:

-(UIImage*)ExtractImageOn:(CGPoint)pointExtractedImg ofSize:(CGSize)sizeExtractedImg FromSpriteSheet:(UIImage*)imgSpriteSheet
{
    UIImage *ExtractedImage;

    CGRect rectExtractedImage;

    rectExtractedImage=CGRectMake(pointExtractedImg.x,pointExtractedImg.y,sizeExtractedImg.width,sizeExtractedImg.height);

    CGImageRef imgRefSpriteSheet=imgSpriteSheet.CGImage;

    CGImageRef imgRefExtracted=CGImageCreateWithImageInRect(imgRefSpriteSheet,rectExtractedImage);

    ExtractedImage=[UIImage imageWithCGImage:imgRefExtracted];

    CGImageRelease(imgRefExtracted);

    //CGImageRelease(imgRefSpriteSheet); I have commented it because
    // we should not release the object that we don't own.
    // So why do we release imgRefExtracted alone? because it has 
    // name create in its method so the ownership comes to us so we
    // have to release it.

    return ExtractedImage;

}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文