Tag Archives: Cocos2D

Cocos2D中实现截图功能

在Cocos2D中截图有2种方法

  • 从OpenGL的显示数据中直接获取,这样可以截取整个屏幕显示的结果,类似Home+Power的效果
  • 使用CCRenderTexture可以单独设定特定的Sprite或者CCNode进行截取保存

OpenGL截取整个屏幕

需要对CCDirector进行Category扩展,加入以下两个文件

CCDirector+ImageSave.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface CCDirector (ImageSave)

- (UIImage *)screenShotUIImage;
- (CCTexture2D *)screenShotTexture2D;

@end

CCDirector+ImageSave.m

#import "CCDirector+ImageSave.h"

@implementation CCDirector (ImageSave)

- (UIImage*) screenShotUIImage {
CGSize size = [self displaySizeInPixels];
//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, [self winSize].width, [self winSize].height, 8, [self winSize].width*4, CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGContextTranslateCTM(context,0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);

switch ([self deviceOrientation]) {
case CCDeviceOrientationPortrait:
break;
case CCDeviceOrientationPortraitUpsideDown:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(180));
CGContextTranslateCTM(context,-size.width, -size.height);
break;
case CCDeviceOrientationLandscapeLeft:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(-90));
CGContextTranslateCTM(context,-size.height, 0);
break;
case CCDeviceOrientationLandscapeRight:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(90));
CGContextTranslateCTM(context,size.width*0.5, -size.height);
break;
}
CGContextDrawImage(context, CGRectMake(0.0, 0.0, size.width, size.height), iref);
UIImage *outputImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];

//Dealloc
CGDataProviderRelease(provider);
CGImageRelease(iref);
CGContextRelease(context);
free(buffer);
free(pixels);

return outputImage;
}

- (CCTexture2D*) screenShotTexture2D {
return [[CCTexture2D alloc] initWithImage:[self screenShotUIImage]];
}

@end

使用的时候,如下就可以保存图片

#import "CCDirector+ImageSave.h"

/*
...
...
*/

UIImage *screenshot = [[CCDirector sharedDirector] screenShotUIImage];
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Screenshot.png"];
[UIImagePNGRepresentation(screenshot) writeToFile:savePath atomically:YES];

CCRenderTexture截图部分CCNode图象

CCRenderTexture是Cocos2D定义的类,所以直接使用吧

// 设定截图大小
CCRenderTexture  *target = [CCRenderTexture renderTextureWithWidth:320 height:480];
[target begin];

// 添加需要截取的CCNode
[someNode visit];

[target end];
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Screenshot.png"];
[target saveBuffer:savePath format:kCCImageFormatPNG];
Tagged ,