I am trying to print the R, G and B value to a label, however It wont print correctly to the label, it just prints the R value with a random set of numbers each time. The error is "Format specifies 'unsigned int' but the argument has type 'char*'
I am new to ios development so help would be appreciated.
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");
}
This gets the pixels of the image
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
The error has gone however it just prints out a blank result, Anyone know why?
答案 0 :(得分:0)
您正在使用格式说明符,如下所示:
[NSString stringWithFormat:@"R= %3.0u", "G= 3.0u", "B= 3.0u", R(color), G(color), B(color)];
此函数需要在第一个参数中应替换的所有参数。问题是第二个参数。 这是第一个论点。
@"R= %3.0u"
下一个参数应该是替换%3.0u 的值,下一个参数是字符串&#34; G = 3.0u&#34; 强烈>这就是为什么它会给你警告。用以下代码替换您的代码:
[NSString stringWithFormat:@"R= %3.0u G= %3.0u B= %3.0u", R(color), G(color), B(color)];
然后你很高兴。如需进一步咨询:
https://developer.apple.com/reference/foundation/nsstring/1497275-stringwithformat