简体   繁体   中英

OpenGL ES Buffer in iOS?

I'm having a strange issue with image captures from OpenGL on iOS. I have used Apple's GLPaint sample code to create a view that allows a user to annotate an image. The resulting painting views are then added to various UITableViewCells as part of user input forms. On completion of the form, the user's annotation is captured to a UIImage with the following sampling code (within my PaintingView).

- (UIImage*)getImage {

    // calculate buffer size
    NSInteger dataLength = backingWidth * backingHeight * 4;
    void *buffer = (GLubyte*)malloc(dataLength);
    GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));

    // transfer image from frame buffer
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glPixelStorei(GL_PACK_ALIGNMENT, 4);
    glReadPixels(0, 0, backingWidth, backingHeight, GL_RGBA, GL_UNSIGNED_BYTE, data);

    // capture image
    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGImageRef imageRef = CGImageCreate(backingWidth, backingHeight, 8, 32, backingWidth * 4, colorSpace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast, ref, NULL, true, kCGRenderingIntentDefault);
    UIImage *image = [UIImage imageWithCGImage:imageRef scale:1.0f orientation:UIImageOrientationDownMirrored];

    // clean up
    CGImageRelease(imageRef);
    CGColorSpaceRelease(colorSpace);
    CGDataProviderRelease(ref);
    free(buffer);
    free(data);

    // capture image
    if( image == nil)
        NSLog(@"Save EAGLImage failed to bind data to a UIImage");

    return image;
}

Within my application, a user can switch between two forms that both contain a single PaintingView used for annotation. If a user submits the initial form, or switches to the secondary form, image capture works just fine. If a user submits the initial form then switches to the secondary from, however, artifacts from the original annotation appear on the second form.

Here, a user annotates and submits the initial form. 初次提交表格

Here, as soon as a user starts annotating the secondary form, artifacts from the initial form appear. 编辑后的辅助表格

The issue seems to be related to the sampling code since it only appears after sampling (I can switch between forms and annotate each without problem as long as I don't sample first). Any ideas what's going on here?

I'm not sure exactly what the issue was here (it appears that the buffer memory was being corrupted, but I couldn't locate where address spaces might be overlapping).

The original Apple code was using layoutSubviews to dynamically resize the painting surface should the layout change--and was deleting and recreating the frame buffers on every call. Because I placed the painting view in a table view cell, layoutSubviews was called several times during scrolling causing the buffers to be created and deleted multiple times needlessly. It seems that somewhere through these multiple calls the buffer address space was begin corrupted.

After moving buffer allocation to my initialization routine, so that it's only performed once, everything works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM