iphone - iPhone上如何从Bitmap Context中提取RGB分量?

标签 iphone image colors bitmap rgb

我尝试使用以下代码从 CGBitmapContext 获取 ARGB 组件:


-(id) initWithImage: (UIImage*) image   //create BitmapContext with UIImage and use 'pixelData' as the pointer
{

        CGContextRef    context = NULL;
        CGColorSpaceRef colorSpace;
        int             bitmapByteCount;
        int             bitmapBytesPerRow;

        bitmapBytesPerRow   = (image.size.width * 4);                       
        bitmapByteCount     = (bitmapBytesPerRow * image.size.height);

        colorSpace = CGColorSpaceCreateDeviceRGB();                         
        pixelData = malloc( bitmapByteCount );      //unsigned char* pixelData is defined in head file
        context = CGBitmapContextCreate (pixelData,                         
                        image.size.width,
                        image.size.height,
                        8,    // bits per component
                        bitmapBytesPerRow,
                        colorSpace,
                        kCGImageAlphaPremultipliedFirst
                        );
        CGColorSpaceRelease( colorSpace );                                  

        CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
        pixelData = CGBitmapContextGetData(context);
        return self;
}


-(float) alphaAtX:(int)x y:(int)y   //get alpha component using the pointer 'pixelData' 
{
    return pixelData[(y *width + x) *4 + 3];    //+0 for red, +1 for green, +2 for blue, +3 for alpha
}<p></p>

<p>-(void)viewDidLoad {
    [super viewDidLoad];
    UIImage *img = [UIImage imageNamed:@"MacDrive.png"];    //load image</p>

<pre><code>[self initWithImage:img];   //create BitmapContext with UIImage
float alpha = [self alphaAtX:20 y:20];  //store alpha component
</code></pre>

<p>}
</p>

当我尝试存储红/绿/蓝时,结果它们始终为 240。而 alpha 始终为 255。

所以我认为指针可能有问题。它无法返回我想要的正确 ARGB 数据。关于代码有什么问题有什么想法吗?

最佳答案

首先,您正在泄漏内存,pixelData 内存永远不会被释放。

为了您的使用,最好让 CGContext 调用来管理内存。只需传递 NULL iso. PixelData,只要您保持上下文的引用计数,CGBitmapContextGetData(context) 将保持有效。

您使用 alpha 就好像它是最后一个条目(RGBA,而不是 ARGB),在创建上下文时使用它,或者调整 alphaAtX 代码。我不确定您是否想要预乘,如果只是为了检查 alpha 值,则不需要。

总而言之:

[...]
CGContextRelease(context);
context = CGBitmapContextCreate (NULL,                                                     
                                    image.size.width,
                                    image.size.height,
                                    8,    // bits per component
                                    bitmapBytesPerRow,
                                    colorSpace,
                                    kCGImageAlphaLast
                                    );
CGColorSpaceRelease( colorSpace );                                                                      

CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
pixelData = CGBitmapContextGetData(context);
[...]

将上下文作为成员变量并初始化为 NULL,已经是朝着正确方向迈出的一步。

关于iphone - iPhone上如何从Bitmap Context中提取RGB分量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/561663/

相关文章:

iOS网络应用程序: Use back swipe gesture to go back

jquery - 如何确保图像加载到 JQuery 插件中?

c# - 使用 Worksheet.Range.Autofilter(Field,Criteria,Operator) 按特定颜色索引/RGB 过滤范围

android - 如何从输入法服务更改导航栏的颜色?

linux - 如何创建像 debian 安装程序一样简单的命令行 UI?

php - MDM DeviceLock 负载错误

ios - UITableViewCell 度假村无需重新加载整个表格

iphone - 如何将 NSData 作为 NSString 传递并取回?

css - 最大高度为 :100% stretches table with manually set height 的图片

html - 使用 float 和边距 : auto 的 TinyMCE 怪异图像对齐