2

我从一个 iOS 游戏(一个 png 文件和一个 plist 文件)中获得了一些图像资源。资源由纹理打包器打包。我想将 .png 和 .plist 文件恢复为 png 图像,但我不知道该怎么做。 我得到的资源

4

1 回答 1

1

cocos2d前段时间我写了一个小项目来实现这一点。您基本上用于CCSpriteFrameCache加载 plist 信息,然后遍历每个spriteFrame以使用CCRenderTexture. 主要逻辑是这样的。-

-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) {

        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:PLIST_FILE];

        NSDictionary *frames = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrames];

        for(id key in frames) {
            //NSLog(@"key=%@ value=%@", key, [frames objectForKey:key]);
            [self saveSpriteToFile:key inFolder:FOLDER_PATH];
        }

    }
    return self;
}

-(void) saveSpriteToFile:(NSString *)name inFolder:(NSString *) folder {
    CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:name];
    CGSize spriteSize = [sprite contentSize];
    float scale = 1;
    int nWidth = spriteSize.width;
    int nHeight = spriteSize.height;
    nWidth *= scale;
    nHeight *= scale;
    [sprite setPosition:ccp(spriteSize.width / 2, spriteSize.height / 2)];
    [sprite setScale:scale];
    [self addChild:sprite];

    CCRenderTexture* render = [CCRenderTexture renderTextureWithWidth:sprite.contentSize.width height:sprite.contentSize.height];
    [render begin];
    [sprite visit];
    [render end];
    //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //NSString *documentsDirectory = [paths objectAtIndex:0];
    [render saveToFile:[NSString stringWithFormat:@"%@/%@", folder, name] format:kCCImageFormatPNG];

    [self removeChild:sprite cleanup:YES];
}

以防万一其他人发现它有用,我刚刚将整个项目上传到 github。-

https://github.com/zuinqstudio/atlasSplitter

希望能帮助到你。

于 2013-08-19T17:11:29.223 回答