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

原创粉丝点击