【问题标题】:Incompatible pointer types passing regardless of type?无论类型如何传递不兼容的指针类型?
【发布时间】:2015-02-11 01:59:05
【问题描述】:

所以我有以下代码,并且在 const double colorMasking[6] 的行上,现在它是一个双精度但如果我清理并构建它说不兼容的指针类型传递双精度应该是浮点数。但是,如果我将其更改为浮动,错误就会消失,但是一旦我清理并再次构建,它就会说传递浮点数的不兼容指针类型应该是双倍的。和我刚刚做的完全相反。知道这里发生了什么吗?

-(UIImage *)changeWhiteColorTransparent: (UIImage *)image
{
    CGImageRef rawImageRef=image.CGImage;

    const double colorMasking[6] = {222, 255, 222, 255, 222, 255};

    UIGraphicsBeginImageContext(image.size);
    CGImageRef maskedImageRef=CGImageCreateWithMaskingColors(rawImageRef, colorMasking);
    {
        //if in iphone
        CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, image.size.height);
        CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
    }

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), maskedImageRef);
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    CGImageRelease(maskedImageRef);
    UIGraphicsEndImageContext();
    return result;
}

【问题讨论】:

  • 愚蠢的问题:该错误消息标记了哪一行。
  • 如果colorMaskingCGFloat 而不是double,它是否有效?
  • @HotLicks 我认为是CGImageRef maskedImageRef=CGImageCreateWithMaskingColors(rawImageRef, colorMasking);,因为这是他唯一一次通过colorMasking
  • 我也想知道用int初始化float数组是否有效。
  • 文档提供:CGImageRef CGImageCreateWithMaskingColors ( CGImageRef image, const CGFloat components[] ); 所以colorMasking 应该是CGFloat 类型。

标签: ios objective-c xcode pointers cgfloat


【解决方案1】:

改变

const double colorMasking[6] = {222, 255, 222, 255, 222, 255};

const CGFloat colorMasking[6] = {222, 255, 222, 255, 222, 255};

CGImageCreateWithMaskingColors 需要 CGFloat,在 32 位系统上是 typedeffloat,在 64 位系统上是 double。使用float编译时:

  1. 编译器编译 32 位二进制文​​件并查看您的 float 数组,这正是函数所期望的。
  2. 编译器编译 64 位二进制文​​件并查看您的 float 数组,但该函数需要一个 double 数组。

当您使用double 而不是float 时会发生相反的情况。

这里是CGFloat的定义(在CoreGraphics/CGBase.h):

#if defined(__LP64__) && __LP64__
# define CGFLOAT_TYPE double
# define CGFLOAT_IS_DOUBLE 1
# define CGFLOAT_MIN DBL_MIN
# define CGFLOAT_MAX DBL_MAX
#else
# define CGFLOAT_TYPE float
# define CGFLOAT_IS_DOUBLE 0
# define CGFLOAT_MIN FLT_MIN
# define CGFLOAT_MAX FLT_MAX
#endif

typedef CGFLOAT_TYPE CGFloat;

【讨论】:

    【解决方案2】:

    文档提供:CGImageRef CGImageCreateWithMaskingColors ( CGImageRef image, const CGFloat components[] ); 所以colorMasking 应该是CGFloat 类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多