无论类型如何传递不兼容的指针类型?

Incompatible pointer types passing regardless of type?

所以我有以下代码,在 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;
}

改变

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

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

CGImageCreateWithMaskingColors 需要一个 CGFloat,它在 32 位系统上 typedefed 为 float,在 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;

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

类型