Instantly share code, notes, and snippets.

Embed
What would you like to do?
A cocos2d-objc method to save localized screenshot images
/**
* This assumes the test is being run on an iPad Pro (or simulator) so the largest screenshot
* doesn't get clipped. Base images with no text should be provided in the test resources with the -ipadhd extension.
* The output will be images in localized subfolders within the Documents folder of the simulator's sandbox.
*
* Run this method in any CCScene or full screen CCNode
*/
- (void)screenGrabAppStoreImages
{
// Our blank images are named "3.5_00-ipadhd.png", "3.5_01-ipadhd.png", "4_00-ipadhd.png", etc.
NSArray *deviceNames = @[@"3.5", @"4", @"4.7", @"5.5", @"ipad", @"ipadhd", @"iPadPro"];
// These are static positions/sizes for our labels for each device name above. Yours will be different!
NSArray *widths = @[@200, @200, @240, @300, @260, @500, @500];
NSArray *heights = @[@32, @32, @40, @44, @44, @88, @88];
NSArray *fontSizes = @[@14, @14, @17, @32, @18, @36, @36];
NSArray *topPadding = @[@20, @20, @20, @40, @20, @40, @40];
[deviceNames enumerateObjectsUsingBlock:^(NSString *deviceName, NSUInteger idx, BOOL * _Nonnull stop) {
CCSprite *baseImage = [CCSprite spriteWithImageNamed:[NSString stringWithFormat:@"%@_01.png", deviceName]];
CCRenderTexture *renderTexture = [CCRenderTexture renderTextureWithWidth:baseImage.width
height:baseImage.height];
for (int i = 0; i < 5; i++)
{
NSString *identifier = [NSString stringWithFormat:@"%@_%02d", deviceName, i];
CCSprite *image = [CCSprite spriteWithImageNamed:[NSString stringWithFormat:@"%@.png", identifier]];
image.anchorPoint = CGPointZero;
[self addChild:image];
// Localizable.strings should contain "Caption_1", "Caption_2", etc keys for each screenshot caption
NSString *captionKey = [NSString stringWithFormat:@"Caption_%d", i + 1];
// Use RSULocalizationHelper from https://github.com/RareSloth/RSUtilities
NSString *captionText = RSULocalizedStringWithDefault(captionKey, @"");
// labelSized() is just a method we use for quickly instantiating a label with a given string, size, and color
CCLabelTTF *label = labelSized(captionText, 0, FURTextColorPurple);
label.fontSize = [fontSizes[idx] floatValue];
label.dimensions = CGSizeMake([widths[idx] floatValue], [heights[idx] floatValue]);
label.adjustsFontSizeToFit = YES;
label.verticalAlignment = CCVerticalTextAlignmentCenter;
label.horizontalAlignment = CCTextAlignmentCenter;
label.anchorPoint = ccp(0.5f, 1.f);
label.position = ccp(image.contentSize.width / 2.f, image.contentSize.height - [topPadding[idx] floatValue]);
[image addChild:label];
[renderTexture beginWithClear:0 g:0 b:0 a:0];
[self visit];
[renderTexture end];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *currentLanguage = _currentLanguage;
/** This will depend on how you have your Localizable.strings set up.
* We had a pt.lproj folder, but the locale on the app store is pt-BR.
* Same with nl.lproj and de.lproj */
if ([currentLanguage isEqualToString:@"pt"])
{
currentLanguage = @"pt-BR";
}
else if ([currentLanguage isEqualToString:@"nl"])
{
currentLanguage = @"nl-NL";
}
else if ([currentLanguage isEqualToString:@"de"])
{
currentLanguage = @"de-DE";
}
NSString *filename = [NSString stringWithFormat:@"%@_%@.png", currentLanguage, identifier];
NSString *filePath = [[paths firstObject] stringByAppendingPathComponent:filename];
[UIImagePNGRepresentation([renderTexture getUIImage]) writeToFile:filePath atomically:YES];
[self removeChild:image];
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment