http://blog.csdn.net/trandy/article/details/7826429
在开发游戏时,往往会有这么一个需求:在某个成就达成或者破纪录时,需要截个屏,然后发送到微博上与好友/粉丝分享,虽然home + 开机键组合可手动截屏,在Cocos2d有个CCRenderTexture类,借助该类可很容易实现代码截取功能。使用CCRenderTexture,我们可以截取游戏场景、某个Layer,甚至是精灵:
-
-
-
- - (void)snapshotScreen:(CCNode*)node
- {
-
- CGSize winSize = [[CCDirector sharedDirector]winSize];
- CCRenderTexture* renderTexture = [CCRenderTexture renderTextureWithWidth:winSize.width
- height:winSize.height];
- [renderTexture begin];
- [node visit];
- [renderTexture end];
- [renderTexture cleanup];
- UIImage *snapshot = [renderTexture getUIImageFromBuffer];
-
- UIImageWriteToSavedPhotosAlbum(snapshot, nil, nil, nil);
- }
如果游戏支持高清模式,上面代码截出来的图是960 * 640大小的,这个尺寸如果要上传到微博上,文件的大小可能会超出限制,那么在上传前可用先把截图大小缩小点如480*320,以减少图片体积:
-
- - (UIImage *) scaleFromImage: (UIImage *) image toSize: (CGSize) size
- {
- UIGraphicsBeginImageContext(size);
- [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
阅读(3833) | 评论(0) | 转发(1) |