不兼容的指针类型传递不管类型?

时间:2015-02-11 01:59:06

标签: ios objective-c xcode pointers cgfloat

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

-(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;
}

2 个答案:

答案 0 :(得分:3)

更改

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数组。
  3. 当您使用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;
    

答案 1 :(得分:1)

文档提供:CGImageRef CGImageCreateWithMaskingColors ( CGImageRef image, const CGFloat components[] );因此colorMasking应为CGFloat类型。