【问题标题】:Passing an Objective-C Object into a CGFunctionRef将 Objective-C 对象传递给 CGFunctionRef
【发布时间】:2011-12-20 00:34:03
【问题描述】:

我正在尝试编写一个 CGFunctionRef,它将充当我使用 ARC 的项目中的 CGShadingRef 对象的着色函数。我正在尝试将 NSMutableArray(充满 UIColors)传递到我的 CGFunctionRef 回调中。

这是我的初始化方法

- (void)initInternal {  
    _colors = [[NSMutableArray alloc] init];

    // Creating the colors in this way ensures that the underlying color space is UIDeviceRGBColorSpace
    // and thus has 4 color components: red, green, blue, alpha
    [_colors addObject:[UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f]]; // Red
    [_colors addObject:[UIColor colorWithRed:0.0f green:1.0f blue:0.0f alpha:1.0f]]; // Green
    [_colors addObject:[UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:1.0f]]; // Blue
    [_colors addObject:[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f]]; // White
    [_colors addObject:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f]]; // Black

    // Define the shading callbacks
    CGFunctionCallbacks callbacks;
    callbacks.version = 0;                      // Defaults to 0
    callbacks.evaluate = CGShadingCallback;     // This is our color selection function
    callbacks.releaseInfo = NULL;               // Not used

    // As input to our function we want 1 value in the range [0.0, 1.0].
    // This is our position within the 'gradient'.
    size_t domainDimension = 1;
    CGFloat domain[2] = {0.0f, 1.0f};

    // The output of our function is 4 values, each in the range [0.0, 1.0].
    // This is our selected color for the input position.
    // The 4 values are the red, green, blue and alpha components.
    size_t rangeDimension = 4;
    CGFloat range[8] = {0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f};

    // Create the shading function
    _shadingFunction = CGFunctionCreate(&_colors, domainDimension, domain, rangeDimension, range, &callbacks);   
}

这是我的回调方法

static void CGShadingCallback(void* info, const float* inData, float* outData) {
    // Our colors
    NSMutableArray* colors = (__bridge_transfer NSMutableArray*)info;
    // Position within the gradient, ranging from 0.0 to 1.0
    CGFloat position = *inData;

    // Find the color that we want to used based on the current position;
    NSUInteger colorIndex = position * [colors count];

    // Account for the edge case where position == 1.0
    if (colorIndex >= [colors count])
        colorIndex = [colors count] - 1;

    // Get our desired color from the array
    UIColor* color = [colors objectAtIndex:colorIndex];

    // Copy the 4 color components (red, green, blue, alpha) to outData
    memcpy(outData, CGColorGetComponents(color.CGColor), 4 * sizeof(CGFloat));  
}

这是我的 drawRect 方法

- (void)drawRect:(CGRect)rect {
    CGRect b = self.bounds;
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // Create a simple elliptic path
    CGContextAddEllipseInRect(ctx, b);
    // Set the current path as the clipping path
    CGContextClip(ctx);

    // Create our shading using the function that was defined earlier.
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGShadingRef shading = CGShadingCreateAxial(colorspace,
                                            CGPointMake(CGRectGetMinX(b), CGRectGetMidY(b)),
                                            CGPointMake(CGRectGetMaxX(b), CGRectGetMidY(b)),
                                            _shadingFunction,
                                            true,
                                            true);

    // Draw the shading
    CGContextDrawShading(ctx, shading);


    // Cleanup
    CGShadingRelease(shading);
    CGColorSpaceRelease(colorspace);
}

如果我只在回调方法中使用 __bridge,那么会崩溃
NSMutableArray* 颜色 = (__bridge NSMutableArray*)info; 带有 EXC_BAD_ACCESS。

如果我使用 __bridge_transfer,它会崩溃 CGContextDrawShading(ctx, 阴影); 在带有 EXC_BAD_ACCESS 的 drawRect 中。

【问题讨论】:

  • 这是什么类型的崩溃? Xcode 控制台中是否有异常或抛出任何异常? __bridge_transfer 听起来更正确,但是您如何尝试引用/使用 colors 变量?
  • 崩溃是 EXC_BAD_ACCESS。你是什​​么意思我要如何引用变量?
  • 通过“引用”,我的意思是显示CGShadingCallback 中的附加行,显示如何使用colors 变量......以及发生崩溃的位置等。
  • 我编辑它包括其余的相关代码。

标签: iphone c core-graphics automatic-ref-counting


【解决方案1】:

您创建一个数组并将该数组用作CGFunction 的上下文。

所以当给函数时需要保留该数组。您可以使用 __bridge_retained 关键字来做到这一点:

CGFunctionCallbacks callbacks;
callbacks.version = 0;
callbacks.evaluate = CGShadingCallback;
callbacks.releaseInfo = myReleaseCallback;

_shadingFunction = CGFunctionCreate((__bridge_retained void *)_colors, domainDimension, domain, rangeDimension, range, &callbacks);

那么,你必须在绘图回调中使用__bridge_transfer__bridge_transfer 将该值转换为强引用而不保留它(所有权已转移)。这相当于释放数组。由于您的回调可能会被多次调用,这不是释放数组的正确位置。

函数销毁时必须释放数组。这就是releaseInfo回调的目的:

static void myReleaseCallback(void *info) {
    CFRelease(info);
}

您也可以使用 __bridge_transfer 演员来做到这一点,但这不是很优雅:

static void myReleaseCallback(void *info) {
    NSArray *array = (__bridge_transfer NSArray *)info;
    // now we need to do something with the array if we don't want a compiler warning
}

【讨论】:

  • 替代解决方案: 由于数组存储在一个强实例变量中,您还可以假设该变量将在CGFunction 的整个生命周期内保留它。在这种情况下,您可以在没有所有权转移的情况下执行所有转换(即使用__bridge)并且不需要提供释放回调。
  • 这行得通。看来我必须更多地阅读ARC。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-06
  • 2015-05-21
  • 2018-11-09
  • 1970-01-01
相关资源
最近更新 更多