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.
Asked
Active
Viewed 1,320 times
2
-
Did you get them from a published app? In that case be aware that those assets are protected by copyright. At best you can use them as placeholders for your internal work. If you got them from elsewhere, the best and easiest way is to get the original files from the author. – CodeSmile Aug 19 '13 at 21:07
1 Answers
1
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.

ssantos
- 16,001
- 7
- 50
- 70
-
This won't be a 1:1 pixel- and color-accurate extraction though. There can be errors in terms of slightly altered colors and if you don't (and I don't see that in this code example) turn off texture filtering the texture will be filtered images (ie images with a smoothing filter applied to them). And render texture has an odd 1-pixel-off bug - not sure when that occurs or whether that has been fixed though. – CodeSmile Aug 19 '13 at 21:05