Iphone 如何捕获同时包含cocs2d和UIKit的视图的屏幕截图

Iphone 如何捕获同时包含cocs2d和UIKit的视图的屏幕截图,iphone,ios,ipad,cocos2d-iphone,ios6,Iphone,Ios,Ipad,Cocos2d Iphone,Ios6,我正在iOS6中开发一个iPad应用程序,它展示了不同颜色和纹理的房屋设计。为此,我使用cocos2d。为了在家中显示使用过的纹理和颜色,我使用UIKit视图。 现在我想截取这个视图的屏幕截图,它包含cocos2d层和UIKit视图。 如果我使用cocos2d拍摄屏幕快照,如: UIImage *screenshot = [AppDelegate screenshotWithStartNode:n]; 然后,只需对cocos2d层进行快照 否则,如果我使用UIkit拍摄屏幕快照,如: UIIm

我正在iOS6中开发一个iPad应用程序,它展示了不同颜色和纹理的房屋设计。为此,我使用cocos2d。为了在家中显示使用过的纹理和颜色,我使用UIKit视图。 现在我想截取这个视图的屏幕截图,它包含cocos2d层和UIKit视图。 如果我使用cocos2d拍摄屏幕快照,如:

UIImage *screenshot = [AppDelegate screenshotWithStartNode:n];
然后,只需对cocos2d层进行快照

否则,如果我使用UIkit拍摄屏幕快照,如:

UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
然后它只捕获UIKit组件并关闭cocos2d部分


我希望它们都在同一屏幕截图中…

尝试这种方法,只需根据需要更改一些代码即可

-(UIImage*) screenshotUIImage
{
    CGSize displaySize  = [self displaySize];
    CGSize winSize      = [self winSize];

    //Create buffer for pixels
    GLuint bufferLength = displaySize.width * displaySize.height * 4;
    GLubyte* buffer = (GLubyte*)malloc(bufferLength);

    //Read Pixels from OpenGL
    glReadPixels(0, 0, displaySize.width, displaySize.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    //Make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);

    //Configure image
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * displaySize.width;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    CGImageRef iref = CGImageCreate(displaySize.width, displaySize.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    uint32_t* pixels = (uint32_t*)malloc(bufferLength);
    CGContextRef context = CGBitmapContextCreate(pixels, winSize.width, winSize.height, 8, winSize.width * 4, CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

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

    switch (deviceOrientation_)
    {
        case CCDeviceOrientationPortrait: break;
        case CCDeviceOrientationPortraitUpsideDown:
            CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(180));
            CGContextTranslateCTM(context, -displaySize.width, -displaySize.height);
            break;
        case CCDeviceOrientationLandscapeLeft:
            CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(-90));
            CGContextTranslateCTM(context, -displaySize.height, 0);
            break;
        case CCDeviceOrientationLandscapeRight:
            CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(90));
            CGContextTranslateCTM(context, displaySize.width * 0.5f, -displaySize.height);
            break;
    }

    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, displaySize.width, displaySize.height), iref);
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *outputImage = [UIImage imageWithCGImage:imageRef];

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

    return outputImage;
}

- (Texture2D*) screenshotTexture {
    return [[Texture2D alloc] initWithImage:[self screenshotUIImage]];
}
有关更多信息,请参阅

参考所有答案和评论,这很有趣


我希望这对你有所帮助

我对此做了很多研究。。。目前我找不到任何代码可以截取包含cocos2d和
UIKit
两者的屏幕截图。即使有一些代码可用,但它在AppStore上是不可接受的。因此,如果您使用该代码,您的应用程序将被AppStore拒绝

所以现在,我找到了一个临时解决方案来实现这一点:

首先,我拍摄了cocos2d层的屏幕截图,然后在
UIImageView
中拍摄了该屏幕截图,并在我的屏幕上添加了
UIImageView
,在所有当前
UIView
的后面,这样用户就无法看到该事件:

[self.view insertSubview:imgView atIndex:1];
在索引1处,因为我的cocos2d层位于索引0处。除此之外

现在,cocos2d图片是我的
UIKit
视图的一部分,我用正常的
UIKit
方式拍摄了当前屏幕的截图。我们到了。我现在有了包含这两个视图的截图

这对我现在起作用了。如果有人找到了一个有效的解决方案,那么我们非常欢迎!!! 我会等待任何可行的解决方案。
谢谢大家的帮助

不,亲爱的。。。它只需要cocos2d层的屏幕截图。。。UIKit组件不在快照中…哦,然后尝试给定的链接,我找到任何东西,然后将post@KananVora试试这个,伙计。。也可以从中得到一些想法。。