【问题标题】:Format specifies 'unsigned int' but the argument has type 'char*'格式指定 'unsigned int' 但参数的类型为 'char*'
【发布时间】: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

错误消失了,但是它只是打印出一个空白结果,有人知道为什么吗?

【问题讨论】:

  • 这里用到的RGB函数是什么?
  • 我添加了更多代码,以便您更好地了解我的问题:)
  • 不确定为什么宏会显示为 char *,但请尝试添加演员表。
  • @rmaddy 我怀疑这是一些晦涩难懂的事情,其中​​ 0xFF 的掩码会强制使用 CHAR 或其他东西?
  • 我已经运行了你的例子,警告消失了。我已经改变了一点。

标签: ios objective-c iphone colors uicolor


【解决方案1】:

您使用的格式说明符如下:

[NSString stringWithFormat:@"R= %3.0u", "G= 3.0u", "B= 3.0u", R(color), G(color), B(color)];

此函数需要在第一个参数中替换的所有参数。问题是第二个参数。 这是第一个参数。

 @"R= %3.0u"

下一个参数应该是要替换 %3.0u 的值,下一个参数是字符串 "G= 3.0u" 这就是它的原因正在给你警告。用这个替换你的代码:

[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

【讨论】:

  • 如果你清楚地看到上面发布的代码是 [NSString stringWithFormat:@"R= %3.0u", "G= 3.0u", "B= 3.0u", R(color), G(颜色),B(颜色)];他指定了第二个参数广告字符串。我想你会很熟悉这个 stringwithformat 方法。第二个参数是字符串,这就是为什么它给他 char * 的警告
  • 还是一样的:)。我发现了错误并发布了答案。请看问题:)
  • @MuhammadZohaibEhsan 我在我的问题上添加了输出的打印屏幕,它不会打印我仍然需要的结果。
  • 我找到了答案! ' _resultLabel.text = [NSString stringWithFormat:@"R= %u G= %u B= %u", R(color), G(color), B(color)];'
  • 我很抱歉。一直以来,我都在查看只有一个字符串的 printf 语句。我所有的cmets都基于那条线。我从来没有注意到 stringWithFormat 显然有额外的字符串。很抱歉因为我的疏忽而浪费了人们的时间。
猜你喜欢
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多