使用opengl捕获IOS7屏幕截图返回黑屏



使用 opengl 捕获屏幕截图适用于 7 之前的 iOS 版本。但是在ios7中运行应用程序时,它会返回黑屏。以下是我正在使用的代码段:

-(UIImage *) glToUIImage {
    CGSize size = self.view.frame.size;
    int image_height = (int)size.width;
    int image_width  = (int)size.height;
    NSInteger myDataLength = image_width * image_height * 4;
    // allocate array and read pixels into it.
    GLubyte *buffer = (GLubyte *) malloc(myDataLength);
    glReadPixels(0, 0, image_width, image_height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
    for(int y = 0; y < image_height; y++)
    {
        for(int x = 0; x < image_width * 4; x++)
        {
            buffer2[(image_height - 1 - y) * image_width * 4 + x] = buffer[y * 4 * image_width + 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 * image_width;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    // make the cgimage
    CGImageRef imageRef = CGImageCreate(image_width, image_height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    // then make the uiimage from that
    UIImage *myImage = [UIImage imageWithCGImage:imageRef];
    return myImage;
}

截图功能,将我的 OpenGL 图像与背景图像相结合,并且已保存到"照片"中。

-(UIImage*)screenshot
{
    UIImage *image = [self glToUIImage];
    CGRect pos = CGRectMake(0, 0, image.size.width, image.size.height);
    UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen]scale]);
     [image drawInRect:pos];
    UIImage* final = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    final = [[UIImage alloc] initWithCGImage: final.CGImage
                                                  scale: 1.0
                                            orientation: UIImageOrientationRight];
    // final picture I saved into Photos.
    UIImageWriteToSavedPhotosAlbum(final, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    return final;
}

有没有人使用 ios7 的 opengl 捕获屏幕截图?

已解决问题。更新了 eaglview 类的可绘制属性函数:

eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                               [NSNumber numberWithBool:FALSE],    
                                kEAGLDrawablePropertyRetainedBacking,
                                kEAGLColorFormatRGBA8,    
                                kEAGLDrawablePropertyColorFormat,
                                nil];

:) :)

最新更新