【问题标题】:IOS7 capturing screenshot using opengl returns black screenIOS7使用opengl捕获屏幕截图返回黑屏
【发布时间】:2013-12-30 18:17:26
【问题描述】:

使用 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 for ios7截屏了吗?

【问题讨论】:

  • 是的 opengl 是必需的,因为我正在创建一个基于 AR 的应用程序,这里使用的库使用 opengl 来创建相机视图
  • 不能解决你的问题……但是glToUIImage 到处都是内存泄漏。我数了5。=P
  • 那么使用opengl捕获屏幕截图需要做什么

标签: ios objective-c


【解决方案1】:

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

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

:) :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 2022-01-13
    • 2015-06-20
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多