2

I am developing an iPad application in iOS6 that shows designing of houses with different colors and textures. For that I am using cocos2d. And for showing the used textures and colors on the home, I am using UIKit views. Now I want to take a screenshot of this view, which contains both cocos2d layer and UIKit views. If I am taking screen shot using cocos2d like:

UIImage *screenshot = [AppDelegate screenshotWithStartNode:n];

then it is only taking a snap of the cocos2d layer.

else if I am taking screen shot using UIkit like:

UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();

then it is only taking capture of the UIKit components and blackouts the cocos2d part.

I want both of them in a same screen shot...

Stefan
  • 5,203
  • 8
  • 27
  • 51
Kanan Vora
  • 2,124
  • 1
  • 16
  • 26

2 Answers2

0

try this method and just change some code with your requirement..

-(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]];
}

For More Info see This Link

Refer all answers and comments its very interesting

i hope this help you

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • No dear... It only takes screen shot of the cocos2d layer... UIKit components are not taken in the snap... – Kanan Vora Dec 06 '12 at 12:14
  • oh then try given link i found anything then will post – Paras Joshi Dec 06 '12 at 12:17
  • @KananVora try this also dude.. see the que also may you get some idea from this.. http://stackoverflow.com/questions/962390/capturing-eaglview-content-with-alpha-channel-on-iphone – Paras Joshi Dec 06 '12 at 12:22
0

I researched a lot about this... And at present I can't find any code which can take a screenshot of a screen containing cocos2d and UIKit both together. Even there is some code available but, it is not acceptable on AppStore. So, if u use that code, your app will be rejected from the AppStore.

So for now, I found a temporary solution to achieve this:

First I took the screenshot of my cocos2d layer and then took that screenshot in a UIImageView and added that UIImageView on my screen behind all the present UIViews like this, such that user can't visualize this event:

[self.view insertSubview:imgView atIndex:1];

At index 1 because my cocos2d layer is at index 0. so above that...

Now, that the cocos2d picture is part of my UIKit view, I took the screenshot of my current screen with the normal UIKit way. And there we are. I now had the screenshot containing both the views.

This worked for me for now. If any one finds a valid solution for this, then most welcome!!! I'll be waiting for any feasible solution for this. Thanks all for help!!!

Kanan Vora
  • 2,124
  • 1
  • 16
  • 26