简体   繁体   中英

How can i choose UIImage for glReadPixels?

I have a view with 50 images. Images may overlap. I want (for example) select image number 33 and find pixel color. How can i do this ? PS i use glReadPixels.

Unless you want to kill time applying the image as a texture first, you wouldn't use glReadPixels. You can do this directly from the UIImage instead:

void pixelExamine( UIImage *image )
    {
    CGImageRef colorImage = image.CGImage;
    int width = CGImageGetWidth(colorImage);
    int height = CGImageGetHeight(colorImage);

    uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), colorImage);

    int x;
    int y;
    uint8_t *rgbaPixel;

    for( y = 0; y < height; y++)
        {
        rgbaPixel = (uint8_t *) &pixels[y * width];

        for( x = 0; x < width; x++, rgbaPixel+=4)
            {
            //  rgbaPixel[0] = ALPHA 0..255
            //  rgbaPixel[3] = RED 0..255
            //  rgbaPixel[2] = GREEN  0..255
            //  rgbaPixel[1] = BLUE 0..255
            }
        }

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    free(pixels);
    }

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