【问题标题】:iOS7 can I access images (not textures) from within an atlas added to xCode project?iOS7 我可以从添加到 xCode 项目的图集中访问图像(不是纹理)吗?
【发布时间】:2014-01-08 11:13:59
【问题描述】:

我在我的 xCode 项目中添加了许多图集 (folder.atlas),通常使用它们按名称为我的精灵创建 SKTextures。但是,现在我发现我可能需要在 Sprite Kit 和基于 UIView 的类之间共享一些图像。

    SKTexture* texture = [[SKTextureAtlas atlasNamed:@"Jetpack"] textureNamed:imageName];
//how to get UIImage?

有没有办法让我在运行时从图集中获取 UIImage?

【问题讨论】:

  • 不是真正的重复,因为我要获取的图像是添加到图集的全尺寸图像,而通过截屏我只会得到屏幕上可见的像素。

标签: iphone ios7 uiimage sprite-kit texture-atlas


【解决方案1】:

现在回答这个问题可能为时已晚,但在我们开发 sprite kit 时,这是一个非常常见的问题。苹果没有提供直接解决这个问题的功能。我所做的对我来说效果很好。

我需要从 atlas dynamite 访问任何图像。

NSString *str = imageName;
str = [str stringByReplacingOccurrencesOfString:@".png" withString:@""];

str = [[str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]] componentsJoinedByString:@""];

SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:str];

UIImage *atlasImage;
if (atlas.textureNames.count > 1)
{
    SKTexture *texture = [atlas textureNamed:[atlas.textureNames objectAtIndex:0]];

    atlasImage = [UIImage imageWithCGImage:texture.CGImage];
}

【讨论】:

    【解决方案2】:

    是的,你可以,但我警告你,它不会很漂亮。这里就不写完整代码了,因为太费时间了。

    如果您查看您的应用程序包,您会注意到您所有的 atlas 文件夹都带有附加的“c”(可能用于编译)。

    如果我的项目中有一个名为“graphics.atlas”的图集。在我的包“资源”中,我会找到一个“graphics.atlasc”文件夹。它有两个或多个文件(取决于图集的大小,spritekit 可以将其拆分)。

    文件: 图形.1.png 图形.plist

    两者都可以通过任何基于 UIKit 的代码随意访问。不过,您需要查看 plist 文件并对其进行解析以找到适合您的图像的位置。这并不容易,但绝对可以做到。

    我很想写一个“SKTextureAtlas+UIImage”类,也许以后再说。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 2011-01-25
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    相关资源
    最近更新 更多