【问题标题】:How to get UIImage from EAGLView?如何从 EAGLView 获取 UIImage?
【发布时间】:2010-11-24 02:00:14
【问题描述】:

我正在尝试从我的 EAGLView 中显示的内容中获取 UIImage。有关如何执行此操作的任何建议?

【问题讨论】:

    标签: iphone uikit uiview opengl-es eaglview


    【解决方案1】:

    这是 Quakeboy 代码的清理版本。 我在 iPad 上测试过,效果很好。 改进包括:

    • 适用于任何尺寸的 EAGLView
    • 适用于 Retina 显示器(点刻度 2)
    • 用 memcpy 替换了嵌套循环
    • 清理内存泄漏
    • 将 UIImage 保存在相册中作为奖励。

    在您的 EAGLView 中使用它作为方法:

    -(void)snapUIImage
    {
        int s = 1;
        UIScreen* screen = [ UIScreen mainScreen ];
        if ( [ screen respondsToSelector:@selector(scale) ] )
            s = (int) [ screen scale ];
    
        const int w = self.frame.size.width;
        const int h = self.frame.size.height;
        const NSInteger myDataLength = w * h * 4 * s * s;
        // allocate array and read pixels into it.
        GLubyte *buffer = (GLubyte *) malloc(myDataLength);
        glReadPixels(0, 0, w*s, h*s, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
        // gl renders "upside down" so swap top to bottom into new array.
        // there's gotta be a better way, but this works.
        GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
        for(int y = 0; y < h*s; y++)
        {
            memcpy( buffer2 + (h*s - 1 - y) * w * 4 * s, buffer + (y * 4 * w * s), w * 4 * s );
        }
        free(buffer); // work with the flipped buffer, so get rid of the original one.
    
        // make data provider with data.
        CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
        // prep the ingredients
        int bitsPerComponent = 8;
        int bitsPerPixel = 32;
        int bytesPerRow = 4 * w * s;
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
        CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
        // make the cgimage
        CGImageRef imageRef = CGImageCreate(w*s, h*s, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
        // then make the uiimage from that
        UIImage *myImage = [ UIImage imageWithCGImage:imageRef scale:s orientation:UIImageOrientationUp ];
        UIImageWriteToSavedPhotosAlbum( myImage, nil, nil, nil );
        CGImageRelease( imageRef );
        CGDataProviderRelease(provider);
        CGColorSpaceRelease(colorSpaceRef);
        free(buffer2);
    }
    

    【讨论】:

    • 上面的代码缺少色彩空间和数据提供者的匹配版本,所以我在方法末尾添加了这些以防止泄漏。
    • 另外,buffer2 确实需要手动释放以避免泄漏。这也已修复。
    • 我试过这个,但总是创建一个没有绘图的黑色图像,我做错了什么?
    • @Brodie 检查 OpenGL 是否使用 glGetError() 生成错误。
    • 嗨,伙计们,我也面临这个问题,这种方法只返回黑色图像。请帮帮我。
    【解决方案2】:

    我无法在此处获得其他答案以使其正常工作。

    几天后,我终于找到了一个可行的解决方案。 Apple 提供了从 EAGLView 生成 UIImage 的代码。然后你只需要垂直翻转图像,因为 UIkit 是倒置的。

    Apple 提供的方法 - 修改为在要制作成图像的视图内。

        -(UIImage *) drawableToCGImage 
    {
    GLint backingWidth2, backingHeight2;
    //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, viewRenderbuffer);
    
    // Get the size of the backing CAEAGLLayer
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth2);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight2);
    
    NSInteger x = 0, y = 0, width2 = backingWidth2, height2 = backingHeight2;
    NSInteger dataLength = width2 * height2 * 4;
    GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));
    
    // Read pixel data from the framebuffer
    glPixelStorei(GL_PACK_ALIGNMENT, 4);
    glReadPixels(x, y, width2, height2, 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(width2, height2, 8, 32, width2 * 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 = self.contentScaleFactor;
        widthInPoints = width2 / scale;
        heightInPoints = height2 / scale;
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(widthInPoints, heightInPoints), NO, scale);
    }
    else {
        // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
        widthInPoints = width2;
        heightInPoints = height2;
        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;
    

    }

    还有一种翻转图像的方法

    - (UIImage *) flipImageVertically:(UIImage *)originalImage {
    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:originalImage];
    UIGraphicsBeginImageContext(tempImageView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGAffineTransform flipVertical = CGAffineTransformMake(
                                                           1, 0, 0, -1, 0, tempImageView.frame.size.height
                                                           );
    CGContextConcatCTM(context, flipVertical);
    
    [tempImageView.layer renderInContext:context];
    
    UIImage *flippedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //[tempImageView release];
    
    return flippedImage;
    

    }

    这里有一个指向 Apple 开发页面的链接,我在该页面中找到了第一种方法供参考。 http://developer.apple.com/library/ios/#qa/qa1704/_index.html

    【讨论】:

    • Nate,我把你的答案从那个问题(这是这个问题的副本)移到了这里,这样人们就可以有一个从 OpenGL ES 场景中捕获的各种方法的首选源.
    • @Nate: 有什么方法可以知道 EAGLView 上是否有乱写的内容。我也在尝试截取上述视图的屏幕截图,但如果用户截取屏幕截图或清除屏幕并截取屏幕截图,则会捕获我想要避免的空屏幕。
    【解决方案3】:
    -(UIImage *) saveImageFromGLView
    {
        NSInteger myDataLength = 320 * 480 * 4;
        // allocate array and read pixels into it.
        GLubyte *buffer = (GLubyte *) malloc(myDataLength);
        glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
        // gl renders "upside down" so swap top to bottom into new array.
        // there's gotta be a better way, but this works.
        GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
        for(int y = 0; y <480; y++)
        {
            for(int x = 0; x <320 * 4; x++)
            {
                buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + 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 * 320;
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
        CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
        // make the cgimage
        CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
        // then make the uiimage from that
        UIImage *myImage = [UIImage imageWithCGImage:imageRef];
    
        CGImageRelease( imageRef );
        CGDataProviderRelease(provider);
        CGColorSpaceRelease(colorSpaceRef);
        free(buffer2);
    
        return myImage;
    }
    

    【讨论】:

    • 上面的代码有一些漏洞,所以我强烈建议使用 Bram 的答案中提供的更有效的代码。
    • 我继续从 Bram 的代码中整合了更关键的内存泄漏修复,以防未来的开发人员将此代码复制并粘贴到他们自己的应用程序中。
    • 感谢您修复漏洞!
    • 大家好,我不知道 OpenGL,我也不明白,在这段代码中你指出需要 EAGLView 吗?
    • 我有 GPUImageView,但我无法从中获取 UIImage,我认为简单的方法是从中获取图像......我该怎么做?请有任何建议。附言布拉德,尊重框架 (v)
    【解决方案4】:

    编辑:正如 derianturner 在下面指出的那样,您不再需要渲染图层,您现在可以(并且应该)使用更高级别的 [UIView drawViewHierarchyInRect:]。除此之外;这应该是一样的。

    EAGLView 只是一种视图,其底层CAEAGLLayer 只是一种层。这意味着,用于将视图/图层转换为 UIImage 的 standard approach 将起作用。 (链接的问题是 UIWebview 的事实并不重要;那只是另一种视图。)

    【讨论】:

    • 感谢 Rob 的回答,但我尝试了“标准方法”,但没有成功。即使我的 EAGLView 正确显示了加载到它的纹理,我能够使用标准方法从中提取的唯一 UIImage 是具有空白白色的 UIImage,这正是 EAGLView 在 IB 中的显示方式。这确实很奇怪,因为 EAGLView 只是一种 UIView。我想也许我需要使用 glReadPixels 或其他东西?我正在使用 Apple 示例代码 GLImageProcessing 示例中的 EAGLView。
    • 呃……我确实说得太快了。 OpenGL 的渲染方式与 Quartz 不同。对于那个很抱歉。我相信这个线程会解决你的问题。通读所有消息;他一路上解决了几个错误。我假设您已经知道如何处理 CGContext 并从中获取 CGImage(以及从中获取 UIImage)。 lists.apple.com/archives/mac-opengl/2006//jan/msg00085.html
    • 所有这些答案都已过时且不必要地冗长。该方案只需要4行UIKit代码,见developer.apple.com/library/content/qa/qa1817/_index.html
    • @demianturner 这看起来与链接的答案完全一样(在“标准方法”下。)您还有什么意思吗?
    • 嗨@RobNapier - 是的,我的链接答案不同。这个 SA 页面浪费了我大约 3 个小时的时间。链接的答案(按照链接“标准方法”导致 stackoverflow.com/questions/858788/… )建议您调用 UIView 的 renderInContext 方法。这是不正确的。我链接到的内容给出了正确的答案,即需要 drawViewHierarchyInRect 方法。此页面上的所有答案,我都尝试过,建议使用 OpenGL,这是不必要的。
    【解决方案5】:

    CGDataProviderCreateWithData带有释放回调来释放数据,你应该在哪里进行释放:

    void releaseBufferData(void *info, const void *data, size_t size)
    {
        free((void*)data);
    }
    

    然后像其他示例一样执行此操作,但不要在此处释放数据:

    GLubyte *bufferData = (GLubyte *) malloc(bufferDataSize);
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bufferData, bufferDataSize, releaseBufferData);
    ....
    CGDataProviderRelease(provider);
    

    或者干脆使用CGDataProviderCreateWithCFData而不用释放回调的东西:

    GLubyte *bufferData = (GLubyte *) malloc(bufferDataSize);
    NSData *data = [NSData dataWithBytes:bufferData length:bufferDataSize];
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
    ....
    CGDataProviderRelease(provider);
    free(bufferData); // Remember to free it
    

    有关更多信息,请查看此讨论:

    What's the right memory management pattern for buffer->CGImageRef->UIImage?

    【讨论】:

    • 这是正确的;仅使用回调来释放 Core Graphics 上下文中的数据。
    【解决方案6】:

    使用上述 Brad Larson 的代码,您必须编辑您的 EAGLView.m

    - (id)initWithCoder:(NSCoder*)coder{
        self = [super initWithCoder:coder];
        if (self) {
            CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
            eaglLayer.opaque = TRUE;
            eaglLayer.drawableProperties = 
                [NSDictionary dictionaryWithObjectsAndKeys: 
                    [NSNumber numberWithBool:YES],  kEAGLDrawablePropertyRetainedBacking, 
                    kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
        }
        return self;
    }
    

    你必须改变numberWithBoolYES

    【讨论】:

      猜你喜欢
      • 2010-11-23
      • 2010-12-21
      • 2011-10-26
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2012-08-11
      相关资源
      最近更新 更多