ios, can't get correct pixel color of CGImageRef

∥☆過路亽.° 提交于 2019-12-08 14:39:02

问题


I made a context, then a gradient. After that I am drawing to the context. Then I receive an gradiented image from context and it is correct, I can see it in the debugger. But when I try to get a pixel color for a specific value it isn't correct. I appreciate any help, thanks. Here is the code.

- (UIColor*) getColorForPoint: (CGPoint) point imageWidth: (NSInteger) imageHeight
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // create a gradient

    CGPoint startPoint = CGPointMake(0, 0);
    CGPoint endPoint = CGPointMake(imageHeight, 0);

    // create a bitmap context to draw the gradient to, 1 pixel high.
    CGContextRef context = CGBitmapContextCreate(NULL, imageHeight, 1, 8, 0, colorSpace,     kCGImageAlphaPremultipliedLast);

    // draw the gradient into it
    CGContextAddRect(context, CGRectMake(0, 0, imageHeight, 1));
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);

    // Get our RGB bytes into a buffer with a couple of intermediate steps...
    //      CGImageRef -> CFDataRef -> byte array
    CGImageRef cgImage = CGBitmapContextCreateImage(context);
    CGDataProviderRef provider = CGImageGetDataProvider(cgImage);
    CFDataRef pixelData = CGDataProviderCopyData(provider);

    // cleanup:
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(cgImage);
    CGContextRelease(context);

    const UInt8* data = CFDataGetBytePtr(pixelData);

    // we got all the data we need.
    // bytes in the data buffer are a succession of R G B A bytes

    CGFloat y = 100 * point.y / imageHeight;
    int pixelIndex = (int)y * 4; // 4 bytes per color
    UIColor *color = [UIColor colorWithRed:data[pixelIndex + 0]/255.0f
                                 green:data[pixelIndex + 1]/255.0f
                                  blue:data[pixelIndex + 2]/255.0f
                                 alpha:data[pixelIndex + 3]/255.0f];

    // done fetching color data, finally release the buffer
    CGDataProviderRelease(provider);

    return color;
}

回答1:


I tested your code and the problem seems to be the bit of multiplication done to point.y before calculating pixelIndex:

CGFloat y = 100 * point.y / imageHeight;
int pixelIndex = (int)y * 4; // 4 bytes per color

I removed this from your code, and it works. Also changed point to an int to match the imageWidth parameter, and bounded it to always be 0 <= point < imageWidth:

- (UIColor*) getColorForPoint: (NSInteger) point imageWidth: (NSInteger) imageWidth
{
    point = MIN(imageWidth - 1, MAX(0, point));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // create a gradient

    CGPoint startPoint = CGPointMake(0, 0);
    CGPoint endPoint = CGPointMake(imageWidth, 0);

    // create a bitmap context to draw the gradient to, 1 pixel high.
    CGContextRef context = CGBitmapContextCreate(NULL, imageWidth, 1, 8, 0, colorSpace,     kCGImageAlphaPremultipliedLast);

    // draw the gradient into it
    CGContextAddRect(context, CGRectMake(0, 0, imageWidth, 1));
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);

    // Get our RGB bytes into a buffer with a couple of intermediate steps...
    //      CGImageRef -> CFDataRef -> byte array
    CGImageRef cgImage = CGBitmapContextCreateImage(context);
    CGDataProviderRef provider = CGImageGetDataProvider(cgImage);
    CFDataRef pixelData = CGDataProviderCopyData(provider);

    // cleanup:
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(cgImage);
    CGContextRelease(context);

    const UInt8* data = CFDataGetBytePtr(pixelData);

    // we got all the data we need.
    // bytes in the data buffer are a succession of R G B A bytes

    int pixelIndex = (int)point * 4; // 4 bytes per color
    UIColor *color = [UIColor colorWithRed:data[pixelIndex + 0]/255.0f
                                     green:data[pixelIndex + 1]/255.0f
                                      blue:data[pixelIndex + 2]/255.0f
                                     alpha:data[pixelIndex + 3]/255.0f];

    // done fetching color data, finally release the buffer
    CGDataProviderRelease(provider);

    return color;
}


来源:https://stackoverflow.com/questions/22174368/ios-cant-get-correct-pixel-color-of-cgimageref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!