【发布时间】:2017-09-04 21:59:46
【问题描述】:
我正在尝试将 R、G 和 B 值打印到标签上,但是它不会正确打印到标签上,它只是每次打印带有一组随机数字的 R 值。错误是“格式指定'unsigned int',但参数的类型为'char*'
我是 ios 开发的新手,因此我们将不胜感激。
NSLog(@"RGB Value of each pixel:");
UInt32 * currentPixel = pixels;
for (NSUInteger j = 0; j < height; j++) {
for (NSUInteger i = 0; i < width; i++) {
UInt32 color = *currentPixel;
printf("(R=%3.0u, G=%3.0u, B=%3.0u) ", R(color), G(color), B(color));
ERROR HERE >> _resultLabel.text = [NSString stringWithFormat:@"R= %3.0u", "G= 3.0u", "B= 3.0u", R(color), G(color), B(color)];
currentPixel++;
}
printf("\n");
}
这会获取图像的像素
CGImageRef inputCGImage = [image CGImage];
NSUInteger width = CGImageGetWidth(inputCGImage);
NSUInteger height = CGImageGetHeight(inputCGImage);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
UInt32 * pixels;
pixels = (UInt32 *) calloc(height * width, sizeof(UInt32));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixels, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), inputCGImage);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
#define Mask8(x) ( (x) & 0xFF )
#define R(x) ( Mask8(x) )
#define G(x) ( Mask8(x >> 8 ) )
#define B(x) ( Mask8(x >> 16) )
Here is the image of the output
错误消失了,但是它只是打印出一个空白结果,有人知道为什么吗?
【问题讨论】:
-
这里用到的
R、G和B函数是什么? -
我添加了更多代码,以便您更好地了解我的问题:)
-
不确定为什么宏会显示为
char *,但请尝试添加演员表。 -
@rmaddy 我怀疑这是一些晦涩难懂的事情,其中 0xFF 的掩码会强制使用 CHAR 或其他东西?
-
我已经运行了你的例子,警告消失了。我已经改变了一点。
标签: ios objective-c iphone colors uicolor