Cocos2d-x和OC层次混搭图层截图解决方案(指定大小)
来源:互联网 发布:mac破解sublime text3 编辑:程序博客网 时间:2024/06/03 06:53
一、首先截取OC的描画图层
二、用openGL方式截取cocos2d-x的图层
三、旋转OC图层的截图,因为第一步是竖屏截取方式
四、合并两张UIImage,注意OC图层必须放在后面,因为它又阿尔法透明通道。
五、使用newimage作为合并后的指定大小的截图
一、首先截取OC的描画图层
-(UIImage *) screenShots{ CGSize imageSize = [[UIScreen mainScreen] bounds].size; if (NULL != UIGraphicsBeginImageContextWithOptions) { UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); } else { UIGraphicsBeginImageContext(imageSize); } CGContextRef context = UIGraphicsGetCurrentContext(); for (UIWindow * window in [[UIApplication sharedApplication] windows]) { if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) { CGContextSaveGState(context); CGContextTranslateCTM(context, [window center].x, [window center].y); CGContextConcatCTM(context, [window transform]); CGContextTranslateCTM(context, -[window bounds].size.width*[[window layer] anchorPoint].x, -[window bounds].size.height*[[window layer] anchorPoint].y); [[window layer] renderInContext:context]; CGContextRestoreGState(context); } } UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;}
二、用openGL方式截取cocos2d-x的图层
// allocate array and read pixels into it. GLubyte *buffer = (GLubyte *) malloc(myDataLength); glReadPixels(0, 0, 1024, 768, GL_RGBA, GL_UNSIGNED_BYTE, buffer); // gl renders "upside down" so swap top to bottom into new array. // there's gotta be a better way, but this works. GLubyte *buffer2 = (GLubyte *) malloc(myDataLength); for(int y = 0; y <768; y++) { for(int x = 0; x <1024 * 4; x++) { buffer2[(767 - y) * 1024 * 4 + x] = buffer[y * 4 * 1024 + x]; } } // make data provider with data. CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL); // prep the ingredients int bitsPerComponent = 8; int bitsPerPixel = 32; int bytesPerRow = 4 * 1024; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; // make the cgimage CGImageRef imageRef = CGImageCreate(1024, 768, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);三、旋转OC图层的截图,因为第一步是竖屏截取方式
UIImage *_imageGL; // openGL屏幕截屏 UIImage *_imageOC; // OC图层屏幕截屏 // 根据设备方向旋转图片 UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (orientation == UIInterfaceOrientationLandscapeLeft) { _imageOC = [_imageOC imageRotatedByRadians:M_PI/2 ]; } else if (orientation == UIInterfaceOrientationLandscapeRight) { _imageOC = [_imageOC imageRotatedByRadians:M_PI*1.5 ]; } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { _imageOC = [_imageOC imageRotatedByRadians:-M_PI ]; }
四、合并两张UIImage,注意OC图层必须放在后面,因为它又阿尔法透明通道。
_imageGL = [UIImage imageWithCGImage:imageRef]; CGImageRelease(cgScreen); // 缩放图片 _imageOC = [_imageOC imageByScalingToSize:CGSizeMake(370, 230)]; _imageGL = [_imageGL imageByScalingToSize:CGSizeMake(370, 230)]; UIGraphicsBeginImageContext(CGSizeMake(370, 230)); // GL在上,OC在下,否则无法合成叠加图片 [_imageGL drawInRect:CGRectMake(0,0,370,230)]; [_imageOC drawInRect:CGRectMake(0,0,370,230)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
五、使用newimage作为合并后的指定大小的截图
- Cocos2d-x和OC层次混搭图层截图解决方案(指定大小)
- Cocos2d-x 截图功能
- cocos2d-x 中调用oc和java
- cocos2d-x优化内存和程序大小
- cocos2d-x优化内存和程序大小
- Cocos2d-x Cocos2dxAccelerometer和Cocos2dxGLSurfaceView出错解决方案
- Cocos2d-x Cocos2dxAccelerometer和Cocos2dxGLSurfaceView出错解决方案
- cocos2d-x android和ios同步解决方案
- cocos2d-x 截图的方法
- 【Cocos2d-x】截图分享功能
- 【Cocos2d-x】截图分享功能
- Cocos2d-x::截图保存、CCArray
- Cocos2d-x:截图背景空白
- OC和cocos2d-x内存管理机制的简单比喻
- cocos2d-x 调用iOS摄像头 c++ 和 OC混编
- cocos2d-x + SmartFoxServer解决方案(安卓)
- cocos2d-x scoket 解决方案
- cocos2d-x scoket 解决方案
- 页面js脚本内,使用字符型服务端动态绑定的值
- 项目计划书格式
- vb.net机房收费系统-错误是我们的必由之路
- 用EXCEL打开TXT文件的办法
- Python利用ConfigParser读取配置文件
- Cocos2d-x和OC层次混搭图层截图解决方案(指定大小)
- 硬盘的存储原理和内部架构
- 莫忘初心,开始博客
- scrollview 实现滑动到底部加载更多数据
- VC6.0 自带小工具 简单介绍
- Unity3D中文视频PDF资源下载地址
- adbd工作原理
- Redhat Linux版本说明
- 库函数strlen的实现