0

I have a method where I'm taking a screenshot, but there's 2 problems with it. For the 2 lines

    CGSize displaySize = [[CCDirector sharedDirector] displaySize];
CGSize winSize = [[CCDirector sharedDirector] winSize];

I get the warning Invalid initializer for displaySize, and also CCDirector may not respond to '-displaySize' Oh and I'm using cocos2d...

This is the entire method

-(UIImage *)screenshot {
CGSize displaySize = [[CCDirector sharedDirector] displaySize];
CGSize winSize = [[CCDirector sharedDirector] winSize];
GLuint bufferLength = displaySize.width * winSize.height * 4;
GLubyte *buffer = (GLubyte *) malloc (bufferLength);
glReadPixels(0, 0, displaySize.width, displaySize.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);

int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * displaySize.width;

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef iref = CGImageCreate (displaySize.width, displaySize.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
uint32_t *pixels = (uint32_t *) malloc (bufferLength);
CGContextRef context = CGBitmapContextCreate(pixels, winSize.width, winSize.height, 8, winSize.width * 4, CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextTranslateCTM(context, 0, displaySize.height);
CGContextScaleCTM(context, 1.0f, -1.0f);

CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, displaySize.width, displaySize.height), iref);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
image = [[[UIImage alloc] initWithCGImage:imageRef] autorelease];

NSString *file = @"GameOver_Screenshot.png";
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [directory stringByAppendingPathComponent:file];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
return image;

}

Joethemonkey101
  • 149
  • 2
  • 18

1 Answers1

0

Looks like "CCDirecrot.h" is not imported to the source file

EDIT: What version of cocos are you using? http://www.cocos2d-iphone.org/api-ref/0.99.5/interface_c_c_director.html Here is no displaySize method for example

Also try to write this CCDirector *director = [CCDirector shareDirector]; and check with debugger director is correct. If director is correct then check with debugger what values are put in winSize and displaySize or when the application is crashing.

Andrew
  • 24,218
  • 13
  • 61
  • 90
  • I didn't import it before, but importing it now didn't change anything :( – Joethemonkey101 Jan 13 '11 at 21:22
  • By the way, all this code is in the AppDelegate...does that make a difference? – Joethemonkey101 Jan 13 '11 at 23:46
  • I added that initialization of director, but what do you want me to do with it? How do I check the values? Sorry for the late response, I didn't see that you updated your answer – Joethemonkey101 Jan 16 '11 at 15:52
  • try putting breakpoint at the next line and when it trigger - hover director variable with a mouse and check if it is nil or not. If everything is correct it should not be nil – Andrew Jan 16 '11 at 16:02
  • Is just simply doing CGSize displaySize; ok? It gets rid of the warning :P If not, what do you mean by hovering the variable? And I should set a breakpoint for the line ____? Thanks for your continued support :) – Joethemonkey101 Jan 16 '11 at 20:42
  • http://developer.apple.com/library/mac/#DOCUMENTATION/DeveloperTools/Conceptual/XcodeDebugging/000-Introduction/Introduction.html read this article. And stop your program with the breakpoint to if director variable is correct – Andrew Jan 16 '11 at 21:11
  • What is 'director' doing? What is the purpose of it? – Joethemonkey101 Jan 16 '11 at 21:50
  • just to check it's ok with a debugger – Andrew Jan 16 '11 at 22:13