ios - 在 iOS 上设置独立于设备的 RGB

标签 ios objective-c ipad pixel color-space

我正在检测点击像素的 rgb。不同的 iPad 返回略有不同的 RGB 值。我维护每个设备返回的不同值的列表,当应用程序打开时,它会确定我所在的设备并使用适当的值。这是一个糟糕的解决方案 - 但它确实有效。

我现在想正确解决这个问题,所以我开始研究 iOS 上的色彩空间。似乎我可以使用 CGColorSpaceCreateCalibratedRGB 来设置标准 RGB,而不管设备如何,所以返回的值是相同的?或者我需要转换

但是,我不知道跨设备创建标准颜色空间所需的任何值,因此我的像素颜色返回值始终相同 - 或者如果可能的话。

一些当前示例返回值:
iPad 2 r31 g0 b133 a1
iPad Air r30 g0 b132 a1

谁能以独立于设备的方式帮助“规范化”像素返回值?

- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
    UIColor* color = nil;
    CGImageRef inImage = self.image.CGImage;
    // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpha, Red, Green, Blue
    CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
    if (cgctx == NULL) { return nil; /* error */ }

    size_t w = CGImageGetWidth(inImage);
    size_t h = CGImageGetHeight(inImage);
    CGRect rect = {{0,0},{w,h}}; 

    // Draw the image to the bitmap context. Once we draw, the memory 
    // allocated for the context for rendering will then contain the 
    // raw image data in the specified color space.
    CGContextDrawImage(cgctx, rect, inImage); 

    // Now we can get a pointer to the image data associated with the bitmap
    // context.
    unsigned char* data = CGBitmapContextGetData (cgctx);
    if (data != NULL) {
        //offset locates the pixel in the data from x,y. 
        //4 for 4 bytes of data per pixel, w is width of one row of data.
        int offset = 4*((w*round(point.y))+round(point.x));
        int alpha =  data[offset]; 
        int red = data[offset+1]; 
        int green = data[offset+2]; 
        int blue = data[offset+3]; 
        ////NSLog(@"offset: %i colors: RGB A %i %i %i  %i",offset,red,green,blue,alpha);
        //NSLog(@"colors: RGB A %i %i %i  %i",red,green,blue,alpha);
        color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
    }


    // When finished, release the context
    CGContextRelease(cgctx); 
    // Free image data memory for the context
    if (data) { free(data); }

    return color;
}


- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {

    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    // Get image width, height. We'll use the entire image.
    size_t pixelsWide = CGImageGetWidth(inImage);
    size_t pixelsHigh = CGImageGetHeight(inImage);

    // Declare the number of bytes per row. Each pixel in the bitmap in this
    // example is represented by 4 bytes; 8 bits each of red, green, blue, and
    // alpha.
    bitmapBytesPerRow   = (pixelsWide * 4);
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

    // Use the generic RGB color space.
    colorSpace = CGColorSpaceCreateDeviceRGB();

    if (colorSpace == NULL)
    {
        fprintf(stderr, "Error allocating color space\n");
        return NULL;
    }

    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL) 
    {
        fprintf (stderr, "Memory not allocated!");
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }

    // Create the bitmap context. We want pre-multiplied ARGB, 8-bits 
    // per component. Regardless of what the source image format is 
    // (CMYK, Grayscale, and so on) it will be converted over to the format
    // specified here by CGBitmapContextCreate.
    context = CGBitmapContextCreate (bitmapData,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedFirst);
    if (context == NULL)
    {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
    }

    // Make sure and release colorspace before returning
    CGColorSpaceRelease( colorSpace );

    return context;
}

最佳答案

如前所述,iOS 不支持它。我也一样,真的可以使用它!

但是,您可以尝试不同的方法。我要做的是将颜色转换为 HSV 空间(或其他一些类似的感知空间),并在你的检查中有一个范围。检查确切的颜色与检查与 float 是否完全相等相同。这不是一个好主意。

您可以使用 [-UIColor getHue:saturation:brightness:alpha:] ( docs here ) 获取 UIColor 的 HSV 组件。 Hue 在 0-1 范围内,但它代表一个角度,因此找到“接近度”需要一点数学知识。但是 saturationvalue 正常工作,因此绝对差异应该足以计算出 2 种颜色彼此之间的接近程度。

关于ios - 在 iOS 上设置独立于设备的 RGB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30126354/

相关文章:

ios - 游戏在快速从后台恢复后退出暂停状态

c# - 如何避免人们使用我在文本框中支持的字符以外的其他字符?

ios - 在 iOS 中获取 CPU 的 I/O 指标

iphone - 除了 MainWindow.xib 之外,是否可以创建没有单独 xib 的通用应用程序?

iphone - iPad UISplitViewController 多 Root View

ios - 在 Objective-C 中的 Swift 类上调用 NSStringFromClass 返回模块错位名称

ios - 创建新的构建配置导致 header 未找到错误

ipad - UIViewControllerHierarchyInconsistency 在 iPad 方向 iOS 6.1.3 期间发生

ios - subview 有委托(delegate),糟糕的设计?

ios - 如何将 UITextField 的值转换为 UITableViewCell 中的 NSString?