最近项目有个需求,获取调色板上某个点的RBGA值,来设置背景颜色。查找了一些资料,经过多次测试,最终拿到的RGBA值基本一致(这里为什么说基本一致呢,如果RGBA值的取值范围在0~255之间的话,结果会有1-2个点的误差)。方法如下:
获取图片上某个点的RGBA值
//获取图片某个点的RGBA值
- (NSMutableArray *)getImagePixel:(UIImage *)image point:(CGPoint)aPoint {
NSMutableArray *byteArray = [[NSMutableArray alloc] initWithCapacity:10];
CGImageRef cgimage = image.CGImage;
size_t scale = CGImageGetWidth(cgimage) / image.size.width;
size_t bpr = CGImageGetBytesPerRow(cgimage); //每行的字节数
size_t bpp = CGImageGetBitsPerPixel(cgimage); //每个像素的位数
size_t bpc = CGImageGetBitsPerComponent(cgimage); //每个字节的组成部分
size_t bytes_per_pixel = bpp / bpc;
// Get CGDataProviderRef from cgimage
CGDataProviderRef provider = CGImageGetDataProvider(cgimage);
@autoreleasepool
{
//Using (__bridge_transfer NSData *) casts the provider to type NSData and gives ownership to ARC, but still not discarded
NSData *data = (__bridge_transfer NSData *)CGDataProviderCopyData(provider);
const UInt8 *bytes = (Byte *)[data bytes];
size_t row = aPoint.y*scale;
size_t col = aPoint.x*scale;
const UInt8* pixel = &bytes[row * bpr + col * bytes_per_pixel];
for(unsigned short i = 0; i < 4; i+=4) {
unsigned short red = pixel[i]; // red channel - unused
unsigned short green = pixel[i+1]; // green channel
unsigned short blue = pixel[i+2]; // blue channel
unsigned short alpha = pixel[i+3]; // alpha channel - unused
[byteArray addObject:@(red)];
[byteArray addObject:@(green)];
[byteArray addObject:@(blue)];
[byteArray addObject:@(alpha)];
}
data = nil;
}
return byteArray;
}
获取图片每个像素点的RGBA值
//获取图片每个像素点的RGBA值
- (NSMutableArray *)getImagePixel:(UIImage *)image point:(CGPoint)aPoint {
// Create array to add all files into total image
NSMutableArray *byteArray = [[NSMutableArray alloc] initWithCapacity:(image.size.width * image.size.height)];
CGImageRef cgimage = image.CGImage;
size_t width = CGImageGetWidth(cgimage);
size_t height = CGImageGetHeight(cgimage);
size_t bpr = CGImageGetBytesPerRow(cgimage); //每行的字节数
size_t bpp = CGImageGetBitsPerPixel(cgimage); //每个像素的位数
size_t bpc = CGImageGetBitsPerComponent(cgimage); //每个字节的组成部分
size_t bytes_per_pixel = bpp / bpc;
// Get CGDataProviderRef from cgimage
CGDataProviderRef provider = CGImageGetDataProvider(cgimage);
@autoreleasepool
{
//Using (__bridge_transfer NSData *) casts the provider to type NSData and gives ownership to ARC, but still not discarded
NSData *data = (__bridge_transfer NSData *)CGDataProviderCopyData(provider);
const UInt8 *bytes = (Byte *)[data bytes];
// Populate byte array with channel data from each pixel
for(size_t row = 0; row < height; row++) {
for(size_t col = 0; col < width; col++) {
const UInt8* pixel = &bytes[row * bpr + col * bytes_per_pixel];
for(unsigned short i = 0; i < 4; i+=4) {
unsigned short red = pixel[i]; // red channel
unsigned short green = pixel[i+1]; // green channel
unsigned short blue = pixel[i+2]; // blue channel
unsigned short alpha = pixel[i+3]; // alpha channel
[byteArray addObject:@(red)];
[byteArray addObject:@(green)];
[byteArray addObject:@(blue)];
[byteArray addObject:@(alpha)];
}
}
}
data = nil;
}
return byteArray;
}