0
我正在使用以下代碼拍攝我的cocos2D iPad應用程序的屏幕截圖。opengl圖層屏幕截圖中的問題
CGSize size = CGSizeMake(WIDTH,HEIGHT);
//Create un buffer for pixels
GLuint bufferLenght=size.width*size.height*4;
GLubyte *buffer = (GLubyte *) malloc(bufferLenght);
//Read Pixels from OpenGL
glReadPixels(0,0,size.width,size.height,GL_RGBA,GL_UNSIGNED_BYTE,buffer);
//Make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLenght, NULL);
//Configure image
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * size.width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef iref = CGImageCreate(size.width,size.height,bitsPerComponent,bitsPerPixel,bytesPerRow,colorSpaceRef,bitmapInfo,provider,NULL,NO,renderingIntent);
uint32_t *pixels = (uint32_t *)malloc(bufferLenght);
CGContextRef context = CGBitmapContextCreate(pixels, WIDTH, HEIGHT, 8, WIDTH*4, CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextTranslateCTM(context,0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
//screen shot of cocos2D layer
CGContextDrawImage(context, CGRectMake(0.0, 0.0, size.width, size.height), iref);
UIImage* screenshot = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
@try
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *documentsDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *paths = [documentsDir objectAtIndex:0];
NSData *myData = UIImageJPEGRepresentation(screenshot, 1.0);
NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:paths traverseLink:YES];
[fileManager createFileAtPath:[paths stringByAppendingPathComponent:@"design.jpg"] contents:myData attributes:fileAttributes];
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil);
}
@catch (NSException *e)
{
//NSLog(@"File error.....");
}
UIGraphicsEndImageContext();
//Dealloc
CGDataProviderRelease(provider);
CGImageRelease(iref);
CGContextRelease(context);
free(buffer);
free(pixels);
此代碼在模擬器上工作,但在實際設備上無法工作。
請幫忙。
謝謝。
當您在設備上運行代碼時是否會引發任何錯誤?你怎麼知道它不在設備上工作? – 2010-12-18 05:13:45
不,沒有給出任何錯誤。並且設備上的結果模糊不清。 – 2010-12-18 06:25:45
完成。問題是我的opengl層的寬度。它的寬度應該是32的倍數,這不是我的情況。 – 2010-12-27 13:50:45