Problem in opengl layer screenshot
I am using following code to take screen-shot of my cocos2D iPad application.
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();
CGBitmapI开发者_运维知识库nfo 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);
This code is working for me on simulator but not working on Actual device.
Please help.
Thanks.
Its done. The problem was with width of my opengl layer. Its width must be in multiple of 32 which was not in my case.
精彩评论