displaySize = 无效的初始值设定项
我有一个截屏的方法,但有两个问题。对于这两行,
CGSize displaySize = [[CCDirector sharedDirector] displaySize];
CGSize winSize = [[CCDirector sharedDirector] winSize];
我收到警告 Invalid Initializer for 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来“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中放入的值或应用程序何时崩溃。
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.