Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何提取图像的RGB值?_Ios_Objective C_Iphone_Ios7 - Fatal编程技术网

Ios 如何提取图像的RGB值?

Ios 如何提取图像的RGB值?,ios,objective-c,iphone,ios7,Ios,Objective C,Iphone,Ios7,而不是使用[uicolorWithPatternImage:[UIImage ImageName:@“my_image.png”]要根据图像设置视图的背景色, 下次我只想使用检索到的RGB值来设置视图的背景色。希望您正在查找我从其他来源复制的图像 + (NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)x andY:(int)y count:(int)count{ NSMutableArray *result = [NSMutableArra

而不是使用
[uicolorWithPatternImage:[UIImage ImageName:@“my_image.png”]
要根据图像设置视图的背景色,
下次我只想使用检索到的RGB值来设置视图的背景色。

希望您正在查找我从其他来源复制的图像

+ (NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)x andY:(int)y count:(int)count{
NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];

// First get the image into your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                bitsPerComponent, bytesPerRow, colorSpace,
                kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

// Now your rawData contains the image data in the RGBA8888 pixel format.
NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
for (int i = 0 ; i < count ; ++i)
{
    CGFloat red   = (rawData[byteIndex]     * 1.0) / 255.0;
    CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
    CGFloat blue  = (rawData[byteIndex + 2] * 1.0) / 255.0;
    CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
    byteIndex += bytesPerPixel;

    UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
    [result addObject:acolor];
}free(rawData);return result;}'
+(NSArray*)getRGBAsFromImage:(UIImage*)图像atX:(int)x andY:(int)y计数:(int)计数{
NSMUTABLEARRY*result=[NSMUTABLEARRY阵列容量:计数];
//首先将图像放入数据缓冲区
CGImageRef imageRef=[图像CGImage];
NSU整数宽度=CGImageGetWidth(imageRef);
NSU整数高度=CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
unsigned char*rawData=(unsigned char*)calloc(高度*宽度*4,sizeof(unsigned char));
NSU整数字节/像素=4;
NSUInteger bytesPerRow=bytesPerPixel*宽度;
NSU整数比特分量=8;
CGContextRef context=CGBitmapContextCreate(原始数据、宽度、高度、,
bitsPerComponent、bytesPerRow、颜色空间、,
KCGIMAGEAlphaPremultipledLast | kCGBitmapByteOrder32Big);
CGCOLORSPACTERELEASE(色彩空间);
CGContextDrawImage(上下文,CGRectMake(0,0,宽度,高度),imageRef);
CGContextRelease(上下文);
//现在,您的rawData包含RGBA8888像素格式的图像数据。
NSUInteger byteIndex=(bytesPerRow*y)+x*bytesPerPixel;
对于(int i=0;i
不清楚您在问什么。