您的位置:首页 > 移动开发 > IOS开发

iOS学习之查看图片某点的ARGB

2015-10-07 10:07 323 查看
坚持 成长 每日一篇

引言

在计算机中,我们可以用ARGB色彩模式来表示一个像素点,每个像素点4个字节,每个字节分别存储ARGB中的一个值。

A:Alpha 透明度(图像通道)

R:Red 红色

G:Green 绿色

B:Blue 蓝色

一种色彩模式,也就是RGB色彩模式附加上Alpha(透明度)通道,常见于32位位图的存储结构。

如果图形卡具有32位总线,附加的8位信号就被用来保存不可见的透明度信号以方便处理用,这就是Alpha通道。白色的alpha象素用以定义不透明的彩色象素,而黑色的alpha象素用以定义透明象素,黑白之间的灰阶用来定义半透明象素。

对于IOS的Image对象我们可以通过控制图片的像素点而达到修改图片颜色,透明度的目的。

实例

1.定义个Color结构体用语保存ARGB

typedef struct {
Byte A;
Byte R;
Byte G;
Byte B;
}Color;


2.定义一个UIImage的类别

@interface UIImage (Color)
-(Byte*)getImageData;
-(Color)GetImageColorAtPointX:(int)X
PointY:(int)Y;
@end

@implementation UIImage (Color)
-(Byte*)getImageData
{
CGImageRef imageRef= [self CGImage];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
int bytesPerPixel = 4;
size_t bytesPerRow=bytesPerPixel*width;
int bitsPerComponent = 8;

void* imageData ;//准备用来存储数据的数组
//创建上下文,kCGImageAlphaPremultipliedLast表示像素点的排序是ARGB
CGContextRef cgContexRef = CGBitmapContextCreate(NULL,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedFirst);

CGRect theRect = CGRectMake(0,0, width, height);
//将图片的数据写入上下文
CGContextDrawImage(cgContexRef, theRect, imageRef);

//Byte* tempData=(Byte*)CGBitmapContextGetData(cgContexRef);
CGColorSpaceRelease(colorSpace);

imageData = malloc(4*(width-1)+4*(height-1)*width+3);
memcpy(imageData, (Byte*)CGBitmapContextGetData(cgContexRef), 4*(width-1)+4*(height-1)*width+3);
//把data做一个深度拷贝,因为原来的数据在cgContexRef里边,要释放,要不会杯具
CGContextRelease(cgContexRef);

return (Byte*)imageData;
}
-(Color)GetImageColorAtPointX:(int)X
PointY:(int)Y{
Byte *imageData = [self getImageData];
//每个像素由四个分量组成所以要乘以4,Y坐标表示有多少列,像素点在数据中的保存方式是从(0,0)开始依次排序,直到图像的(width,height)处的像素点。
int index =4*X+4*Y*self.size.width;
Color c;
c.A=imageData [index];
c.R=imageData[index+1];
c.G=imageData[index+2];
c.B=imageData[index+3];
free(imageData);
return c;
}


类别的使用

UIImage *image = [UIImage imageNamed:@"20150319020925701.jpg"];
Color color = [image GetImageColorAtPointX:314 PointY:65];
NSLog(@"color.A = %d,color.R = %d,color.G =%d,color.B=%d",color.A,color.R,color.G,color.B);


函数解释

创建上下文:

CGContextRef CGBitmapContextCreate(void *data, size_t width,
size_t height, size_t bitsPerComponent, size_t bytesPerRow,
CGColorSpaceRef space, CGBitmapInfo bitmapInfo)


参数:

data : 指向要渲染的绘制内存的地址。这个内存块的大小至少是(bytesPerRow*height)个字节,在ios4.0或者OSX10.6以后可以直接用NULL表示自动分配内存块大小。

width : bitmap的宽度,单位为像素

height : bitmap的高度,单位为像素

bitsPerComponent : 内存中像素的每个组件的位数.例如,对于32位像素格式和RGB 颜色空间,你应该将这个值设为8.

bytesPerRow: bitmap的每一行在内存所占的比特数

colorspace : bitmap上下文使用的颜色空间。

bitmapInfo : 指定bitmap是否包含alpha通道,像素中alpha通道的相对位置,像素组件是整形还是浮点型等信息的字符串。

根据上下文获取数据

void *CGBitmapContextGetData(CGContextRef context)


CGImageAlphaInfo结构体

typedef CF_ENUM(uint32_t, CGImageAlphaInfo) {
kCGImageAlphaNone,               /* For example, RGB. */
kCGImageAlphaPremultipliedLast,  /* For example, premultiplied RGBA */
kCGImageAlphaPremultipliedFirst, /* For example, premultiplied ARGB */
kCGImageAlphaLast,               /* For example, non-premultiplied RGBA */
kCGImageAlphaFirst,              /* For example, non-premultiplied ARGB */
kCGImageAlphaNoneSkipLast,       /* For example, RBGX. */
kCGImageAlphaNoneSkipFirst,      /* For example, XRGB. */
kCGImageAlphaOnly                /* No color data, alpha data only */
};


我们在创建上下文时候对于bitmapInfo我们传入的是CGImageAlphaInfo结构体
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: