我有一个方法,我正在截取屏幕截图,但有2个问题。对于2行
CGSize displaySize = [[CCDirector sharedDirector] displaySize];
CGSize winSize = [[CCDirector sharedDirector] winSize];
我收到displaySize的无效初始化程序警告,并且CCDirector可能无法响应'-displaySize' 哦,我正在使用cocos2d ......
这是整个方法
-(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;
}
答案 0 :(得分:0)
看起来“CCDirecrot.h”未导入源文件
编辑: 您使用的是哪种版本的cocos? http://www.cocos2d-iphone.org/api-ref/0.99.5/interface_c_c_director.html 这里没有displaySize方法,例如
也尝试写这个
CCDirector *director = [CCDirector shareDirector];
并检查调试器主管是否正确。
如果director是正确的,那么请与调试器一起检查winSize和displaySize中的值或应用程序崩溃时的值。