Cocos2d-x和OC层次混搭图层截图解决方案(指定大小)

一、首先截取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作为合并后的指定大小的截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值