简体   繁体   English

获取openGL图像-在模拟器中运行,在iPad上崩溃

[英]Getting an openGL image - runs in simulator, crashes on iPad

The purpose of this function is to return a UIImage from an openGL image. 此函数的目的是从openGL图像返回UIImage。 The reason it's being converted to a CG image is so openGL and UIKit elements can be rendered on top of each other, which is taken care of in another function. 之所以将其转换为CG图像,是因为openGL和UIKit元素可以相互呈现,这在另一个函数中可以解决。

The strange thing is, when the app is run in the simulator, everything works fine. 奇怪的是,当应用程序在模拟器中运行时,一切正常。 However, after testing the app on multiple different iPads, when the drawGlToImage method is called on self , the app crashes with a EXC_BAD_ACCESS code=1 error. 然而,测试在多个不同的iPad该应用中,当后drawGlToImage方法被调用self ,应用程序崩溃与EXC_BAD_ACCESS码= 1个错误。 Does anyone know what I'm doing here that would cause this? 有人知道我在这里做什么会导致这种情况吗? I've read that UIGraphicsBeginImageContext() used to have thread safety issues, but it seems like that was fixed in iOS 4. 我读过UIGraphicsBeginImageContext()曾经有线程安全问题,但似乎在iOS 4中已解决。

    - (UIImage *)drawGlToImage
{
    self.context = [EAGLContext currentContext];
    [EAGLContext setCurrentContext:self.context];
    UIGraphicsBeginImageContext(self.view.frame.size);

    unsigned char buffer[1024 * 768 * 4];
    NSInteger dataSize = 1024 * 768 * 4;

    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    UIGraphicsPushContext(currentContext);

    glReadPixels(0, 0, 1024, 768, GL_RGBA, GL_UNSIGNED_BYTE, &buffer);

    //flip the image
    GLubyte *flippedBuffer = (GLubyte *) malloc(dataSize);

    for(int y = 0; y <768; y++)
    {
        for(int x = 0; x <1024 * 4; x++)
        {
            if(buffer[y* 4 * 1024 + x]==0)
                flippedBuffer[(767 - y) * 1024 * 4 + x]=1;
            else
                flippedBuffer[(767 - y) * 1024 * 4 + x] = buffer[y* 4 * 1024 + x];
        }
    }



    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, flippedBuffer, 1024 * 768 * 4, NULL);
    CGImageRef iref = CGImageCreate(1024,768,8,32,1024*4, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaLast, ref, NULL, true, kCGRenderingIntentDefault);

    CGContextScaleCTM(currentContext, 1.0, -1.0);
    CGContextTranslateCTM(currentContext, 0, -self.view.frame.size.height);

    UIGraphicsPopContext();
    UIImage *image = [[UIImage alloc] initWithCGImage:iref];
    UIGraphicsEndImageContext();

    return image;
    free(flippedBuffer);
    UIGraphicsPopContext();
}

When a button is pressed, a method that is called makes this assignment, which causes the app to crash. 当按下按钮时,将调用一个方法进行分配,这将导致应用程序崩溃。

UIImage *glImage = [self drawGlToImage];

I am not sure in which phase you are calling this method. 我不确定您在哪个阶段调用此方法。 But before calling any OpenGL functions you need to set the right OpenGL context. 但是在调用任何OpenGL函数之前,您需要设置正确的OpenGL上下文。 In the Xcode template it is this line 在Xcode模板中是这一行

[EAGLContext setCurrentContext:self.context];

Here's the code used to solve it 这是用于解决它的代码

- (UIImage *)drawGlToImage {

    // Code borrowed and tweaked from:
    // http://stackoverflow.com/questions/9881143/missing-part-of-the-image-when-taking-screenshot-while-supporting-retina-display

    CGFloat scale = UIScreen.mainScreen.scale;
    CGFloat xOffset = 40.0f;
    CGFloat yOffset = -16.0f;
    CGSize size = CGSizeMake((self.chart.frame.size.width) * scale,
                             self.chart.frame.size.height * scale);

    //Create buffer for pixels
    GLuint bufferLength = size.width * size.height * 4;
    GLubyte* buffer = (GLubyte*)malloc(bufferLength);

    //Read Pixels from OpenGL
    glReadPixels(0.0f, 0.0f, size.width, size.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    //Make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);

    //Configure image
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * size.width;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaLast;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    CGImageRef iref = CGImageCreate(size.width, size.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    uint32_t* pixels = (uint32_t*)malloc(bufferLength);
    CGContextRef context = CGBitmapContextCreate(pixels, size.width, size.height, 8, size.width * 4, CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGContextTranslateCTM(context, 0.0f, size.height);
    CGContextScaleCTM(context, 1.0f, -1.0f);

    // These numbers are a little magical.
    CGContextDrawImage(context, CGRectMake(xOffset, yOffset, ((size.width - (6.0f * scale)) / scale) - (xOffset / 2), (size.height / scale) - (yOffset / 2)), iref);
    UIImage *outputImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];

    //Dealloc
    CGDataProviderRelease(provider);
    CGImageRelease(iref);
    CGContextRelease(context);
    free(buffer);
    free(pixels);

    return outputImage;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用UITableView的应用程序在iPad模拟器上运行,但在iPhone模拟器上崩溃 - App with UITableView runs on iPad simulator but crashes on the iPhone simulator 应用在iPhone6上启动时崩溃,在iPhone4s和iPad Retina和iPhone6模拟器上运行良好 - App crashes at launch on iPhone6, runs fine on iPhone4s and iPad Retina and iPhone6 simulator 应用程序在iPhone上崩溃,但在模拟器或iPad上没有崩溃? - App crashes on iPhone but not simulator or iPad? iPad模拟器iOS6,未从UIImagePickerController照片库获取图像 - iPad simulator iOS6, not getting image from UIImagePickerController photo library iPhone应用程序可在iPad模拟器上运行,但背景是反向的! - iPhone app runs on iPad simulator, but the background is inverted! SQLite Code在iPhone模拟器上运行,在iPad上崩溃 - SQLite Code works on iPhone simulator, crashes on iPad iPhone App可在iPad-Simulator 3.2上运行,但不能在任何其他模拟器上运行 - iPhone App runs on iPad-Simulator 3.2 but not on any other Simulator ipad 应用程序在 ipad 上完美运行但在模拟器中不运行,这是不是一个坏信号? - Is it a bad sign that an ipad app runs perfectly on the ipad but not in the simulator? iPhone模拟器4.3在OpenGL ES应用程序中崩溃 - Iphone simulator 4.3 crashes in opengl es application 当我使用UIImagePicker从iPad的照片库中选择图像时,模拟器崩溃了 - when i pick image from Photos Library in iPad using UIImagePicker, simulator getting crashed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM