【问题标题】:How to capture a screenshot of a view that contains cocs2d and UIKit both如何捕获同时包含 cocs2d 和 UIKit 的视图的屏幕截图
【发布时间】:2012-12-06 11:33:18
【问题描述】:

我正在 iOS6 中开发一个 iPad 应用程序,该应用程序展示了具有不同颜色和纹理的房屋设计。为此,我正在使用 cocos2d。为了在家里显示使用的纹理和颜色,我正在使用 UIKit 视图。 现在我想截取这个视图的截图,其中包含 cocos2d 层和 UIKit 视图。 如果我使用 cocos2d 进行屏幕截图,例如:

UIImage *screenshot = [AppDelegate screenshotWithStartNode:n];

那么它只是拍摄 cocos2d 层的快照。

如果我使用 UIkit 进行屏幕截图,例如:

UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();

那么它只捕获 UIKit 组件并关闭 cocos2d 部分。

我希望他们两个在同一个屏幕截图中......

【问题讨论】:

    标签: iphone ios ipad cocos2d-iphone ios6


    【解决方案1】:

    试试这个方法,然后根据您的要求更改一些代码..

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

    更多信息请见This Link

    参考所有的答案,非常有趣

    希望对你有帮助

    【讨论】:

    • 不,亲爱的……它只截取cocos2d层的屏幕截图……UIKit组件没有被抓拍……
    • 哦,然后尝试给定链接我找到任何东西然后会发布
    • @KananVora 试试这个也老兄.. 看看 que 也可能你从中得到一些想法.. stackoverflow.com/questions/962390/…
    【解决方案2】:

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

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

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

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

    在索引 1,因为我的 cocos2d 层在索引 0。所以在那个之上...

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

    目前这对我有用。如果有人为此找到有效的解决方案,那么非常欢迎!!! 我将为此等待任何可行的解决方案。 谢谢大家的帮助!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多