screenShot代码不适用于ipad,适用于iphone

Posted

技术标签:

【中文标题】screenShot代码不适用于ipad,适用于iphone【英文标题】:screenShot code Not working on ipad, works on iphone 【发布时间】:2011-07-14 11:29:24 【问题描述】:

我遇到了这个奇怪的问题... 我必须捕获屏幕数据并使用以下代码将其转换为图像..此代码在 iphone/ipad 模拟器和 iphone 设备上运行良好,但不适用于 iPad。 iphone 设备是 ios 版本 3.1.1 而 ipad 是 ios 4.2...

- (UIImage *)screenshotImage 
CGRect screenBounds = [[UIScreen mainScreen] bounds];
int backingWidth = screenBounds.size.width;
int backingHeight =screenBounds.size.height;
NSInteger myDataLength = backingWidth * backingHeight * 4;
GLuint *buffer = (GLuint *) malloc(myDataLength);
glReadPixels(0, 0, backingWidth, backingHeight, GL_RGBA4, GL_UNSIGNED_BYTE, buffer);
for(int y = 0; y < backingHeight / 2; y++) 
    for(int xt = 0; xt < backingWidth; xt++) 
        GLuint top = buffer[y * backingWidth + xt];
        GLuint bottom = buffer[(backingHeight - 1 - y) * backingWidth + xt];
        buffer[(backingHeight - 1 - y) * backingWidth + xt] = top;
        buffer[y * backingWidth + xt] = bottom;
    

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, myDataLength, releaseScreenshotData);
const int bitsPerComponent = 8;
const int bitsPerPixel = 4 * bitsPerComponent;
const int bytesPerRow = 4 * backingWidth;

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(backingWidth,backingHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);

UIImage *myImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

// myImage = [self addIconToImage:myImage];
return myImage;    

知道出了什么问题吗..??

【问题讨论】:

【参考方案1】:

这两行不匹配

NSInteger myDataLength = backingWidth * backingHeight * 4;

glReadPixels(0, 0, backingWidth, backingHeight, GL_RGBA4, GL_UNSIGNED_BYTE, buffer);

GL_RGB4 表示每个通道 4 位,但是您为每个通道分配 8 位。正确的令牌是GL_RGB8。在 iPhone 上,GL_RGB4 可能不受支持并回退到 GL_RGBA

还要确保您正在从正确的缓冲区读取数据(正面与左侧与任何(意外)绑定的 FBO)。我建议在进行缓冲区交换之前从后台缓冲区读取数据。

【讨论】:

k 感谢您的快速回复.. 现在我已将 GL_RGB4 更改为 GL_RGBA。代码在 iphone 设备上运行良好,但在 ipad 设备上运行良好不知道是什么问题....我在两个设备上使用相同的代码 将其更改为 GL_RGBA8,而不仅仅是 GL_RGBA(注意“8”)。 嘿,我的 xcode 并没有准确地给我 GL_RGBA8 的选项,而是给了 GL_RGBA8_OES 作为选项..当我使用这个 GL_RGBA8_OES 时,它给出了编译时错误“”GL_RGBA8_OES 没有在这个范围内定义“” @Tornado:包含 OES 标头? Hmmmm .. Opengl 框架包含在项目中...我已经包含了#import .....我还需要导入什么?【参考方案2】:

对于 ios 4 或更高版本,我使用多采样技术进行抗锯齿 ....glReadpixels() 无法直接从 multiSampled FBO 读取,您需要将其解析为单采样缓冲区,然后尝试读取它...请参考到以下帖子:-

Reading data using glReadPixel() with multisampling

【讨论】:

【参考方案3】:

openGL ES 苹果文档截图

- (UIImage*)snapshot:(UIView*)eaglview

    GLint backingWidth, backingHeight;

    // Bind the color renderbuffer used to render the OpenGL ES view
    // If your application only creates a single color renderbuffer which is already bound at this point, 
    // this call is redundant, but it is needed if you're dealing with multiple renderbuffers.
    // Note, replace "_colorRenderbuffer" with the actual name of the renderbuffer object defined in your class.
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, _colorRenderbuffer);

    // Get the size of the backing CAEAGLLayer
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);

    NSInteger x = 0, y = 0, width = backingWidth, height = backingHeight;
    NSInteger dataLength = width * height * 4;
    GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));

    // Read pixel data from the framebuffer
    glPixelStorei(GL_PACK_ALIGNMENT, 4);
    glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);

    // Create a CGImage with the pixel data
    // If your OpenGL ES content is opaque, use kCGImageAlphaNoneSkipLast to ignore the alpha channel
    // otherwise, use kCGImageAlphaPremultipliedLast
    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGImageRef iref = CGImageCreate(width, height, 8, 32, width * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,
                                    ref, NULL, true, kCGRenderingIntentDefault);

    // OpenGL ES measures data in PIXELS
    // Create a graphics context with the target size measured in POINTS
    NSInteger widthInPoints, heightInPoints;
    if (NULL != UIGraphicsBeginImageContextWithOptions) 
        // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
        // Set the scale parameter to your OpenGL ES view's contentScaleFactor
        // so that you get a high-resolution snapshot when its value is greater than 1.0
        CGFloat scale = eaglview.contentScaleFactor;
        widthInPoints = width / scale;
        heightInPoints = height / scale;
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(widthInPoints, heightInPoints), NO, scale);
    
    else 
        // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
        widthInPoints = width;
        heightInPoints = height;
        UIGraphicsBeginImageContext(CGSizeMake(widthInPoints, heightInPoints));
    

    CGContextRef cgcontext = UIGraphicsGetCurrentContext();

    // UIKit coordinate system is upside down to GL/Quartz coordinate system
    // Flip the CGImage by rendering it to the flipped bitmap context
    // The size of the destination area is measured in POINTS
    CGContextSetBlendMode(cgcontext, kCGBlendModeCopy);
    CGContextDrawImage(cgcontext, CGRectMake(0.0, 0.0, widthInPoints, heightInPoints), iref);

    // Retrieve the UIImage from the current context
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    // Clean up
    free(data);
    CFRelease(ref);
    CFRelease(colorspace);
    CGImageRelease(iref);

    return image;

【讨论】:

以上是关于screenShot代码不适用于ipad,适用于iphone的主要内容,如果未能解决你的问题,请参考以下文章

Webview 适用于 iphone 但不适用于 ipad

我逐字复制了 UIPopoverController 代码,该代码适用于 xcode 3.2 上的 ipad,但不适用于 xcode 4.2

JayData / Phonegap 适用于 iPad 模拟器但不适用于 iPad 设备

Swift pushViewController 适用于 iPad 但不适用于 iPhone

videoView 控制器中的摄像机视图适用于 iPhone 但不适用于 iPad

Appdelegate 适用于 iPhone,但不适用于 iPad 版本