如何从BitmapContext在iPhone上拉出ARGB组件? [英] How to pull out the ARGB component from BitmapContext on iPhone?

查看:125
本文介绍了如何从BitmapContext在iPhone上拉出ARGB组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从以下代码从CGBitmapContext获取ARGB组件:

I'm trying to get ARGB components from CGBitmapContext with the following codes:


-(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
}

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

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

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

}

当我尝试存储红色/绿色/蓝色时,它们总是240. alpha是总是255.

When I try to store red/green/blue, they turn out to be always 240. And alpha is always 255.

所以我想也许指针有问题。它不能返回我想要的正确的ARGB数据。关于代码有什么问题的任何想法?

So I think maybe something is wrong with the pointer. It could not return the correct ARGB data I want. Any ideas about what's wrong with the code?

推荐答案

首先,你泄漏的内存,pixelData内存从来没有释放。

First of all, you're leaking memory, the pixelData memory never gets freed.

对于你的使用,最好让CGContext调用管理内存。只需传递NULL i.s.o. pixelData,并且只要你在你的上下文中保持引用计数, CGBitmapContextGetData(context)将保持有效。

For your usage, it's better to let the CGContext calls manage the memory. Simply pass NULL i.s.o. pixelData, and as long as you keep the reference count up on your context, the CGBitmapContextGetData(context) will remain valid.

您正在使用alpha,就像它是最后一个条目(RGBA,而不是ARGB),在创建上下文时使用它,或者修改 alphaAtX 码。我不知道如果你想要预乘,如果它只是为了检查alpha值,你不会。

You're using alpha as if it was the last entry (RGBA, not ARGB), use it as such in the creation of your context, or adapt the alphaAtX code. I'm not sure if you want premultiplied, if it's just to check alpha values, you don't.

所有的东西像:

[...]
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);
[...]

上下文是一个成员变量,已经是朝正确方向迈出的一步。

with context being a member variable, initialized to NULL, will already be a step in the right direction.

这篇关于如何从BitmapContext在iPhone上拉出ARGB组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆