Reputation:
I got some image resources from a iOS game (a png file & a plist file). The resources are packed by texture packer. I'd like to restore the .png & .plist file back to png images, but I don't know how to do this.
Upvotes: 2
Views: 1404
Reputation: 16526
I wrote a little cocos2d
project just to achieve that a time ago. You basically use CCSpriteFrameCache
to load the plist information, and then iterate over each spriteFrame
to 'cut' the desired piece of the atlas with CCRenderTexture
. The main logic looks like this.-
-(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];
}
Just in case anyone else found it useful, I've just uploaded the whole project to github.-
https://github.com/zuinqstudio/atlasSplitter
Hope it helps.
Upvotes: 1