在iOS 12 / Objective C中未调用CGPatternDrawPatternCallback

时间:2018-10-03 20:56:51

标签: ios objective-c core-graphics

以下Objective-C代码在iOS 9-11中正常工作。它绘制带有彩色正方形的棋盘。由于某种原因,iOS 12和Xcode 10.0中未调用添加颜色的回调。我尝试了各种修复程序,但是没有明显的作用。 iOS 12中的某些内容似乎已更改,但我尝试解决的所有问题都没有。

-(id)initWithFrame:(CGRect)frame checkerSize:(CGSize)checkerSize darkColor:(UIColor *)darkShade lightColor:(UIColor *)lightShade { 
    self = [super initWithFrame:frame];
    if(self != nil)
    {
        //  Initialize the property values 
        checkerHeight = checkerSize.height;
        checkerWidth = checkerSize.width;

        self.darkColor  = darkShade;
        self.lightColor = lightShade;


        // Colored Pattern setup
        CGPatternCallbacks coloredPatternCallbacks = {0, ColoredPatternCallback, NULL};
        CGRect clippingRectangle = CGRectMake(0.0, 0.0, 2.0*checkerWidth, 2.0*checkerHeight);

        // First we need to create a CGPatternRef that specifies the qualities of our pattern.
        CGPatternRef coloredPattern = CGPatternCreate(
                                                  (__bridge_retained void *)self,       // 'info' pointer for our callback
                                                  clippingRectangle,                              // the pattern coordinate space, drawing is clipped to this rectangle
                                                  CGAffineTransformIdentity,            // a transform on the pattern coordinate space used before it is drawn.
                                                  2.0*checkerWidth, 2.0*checkerHeight,  // the spacing (horizontal, vertical) of the pattern - how far to move after drawing each cell
                                                  kCGPatternTilingNoDistortion,
                                                  true,                                 // this is a colored pattern, which means that you only specify an alpha value when drawing it
                                                  &coloredPatternCallbacks);            // the callbacks for this pattern.

        // To draw a pattern, you need a pattern colorspace.
        // Since this is an colored pattern, the parent colorspace is NULL, indicating that it only has an alpha value.
        CGColorSpaceRef coloredPatternColorSpace = CGColorSpaceCreatePattern(NULL);
        CGFloat alpha = 1.0;

        // Since this pattern is colored, we'll create a CGColorRef for it to make drawing it easier and more efficient.
        // From here on, the colored pattern is referenced entirely via the associated CGColorRef rather than the
        // originally created CGPatternRef.
        coloredPatternColor = CGColorCreateWithPattern(coloredPatternColorSpace, coloredPattern, &alpha);
        CGColorSpaceRelease(coloredPatternColorSpace);
        CGPatternRelease(coloredPattern);
    }
    return self;
}

void ColoredPatternCallback(void *info, CGContextRef context) {

    HS_QuartzPatternView *self = (__bridge_transfer id)info;    //  needed to access the Obj-C properties from the C function

    CGFloat checkerHeight = [self checkerHeight];
    CGFloat checkerWidth = [self checkerWidth];

    // "Dark" Color

    UIColor *dark = [self darkColor];

    CGContextSetFillColorWithColor(context, dark.CGColor);
    CGContextFillRect(context, CGRectMake(0.0, 0.0, checkerWidth, checkerHeight));
    CGContextFillRect(context, CGRectMake(checkerWidth, checkerHeight, checkerWidth, checkerHeight));

    // "Light" Color
    UIColor *light = [self lightColor];

    CGContextSetFillColorWithColor(context, light.CGColor);
    CGContextFillRect(context, CGRectMake(checkerWidth, 0.0, checkerWidth, checkerHeight));
    CGContextFillRect(context, CGRectMake(0.0, checkerHeight, checkerWidth, checkerHeight));
}

0 个答案:

没有答案