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

时间:2013-12-30 04:55:21

标签: ios objective-c

使用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;
}

是否有人使用opengl为ios7捕获了截图?

1 个答案:

答案 0 :(得分:0)

已解决问题。更新了eaglview类的drawableproperties函数:

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

:):)