为什么在比较ARC下的桥接CGColorRefs时遇到崩溃?

时间:2011-10-16 02:36:56

标签: iphone objective-c ios memory-management automatic-ref-counting

我有一个用于在我的应用中使用的阴影表视图的类。

在转换到ARC时,我不得不改变一些事情。对于新课程,它会崩溃在以下几行:(id)(inverse ? darkColor : lightColor)(__bridge id)(inverse ? lightColor : darkColor)。控制台说*** -[Not A Type retain]: message sent to deallocated instance 0x4cee70

在:

- (CAGradientLayer *)shadowAsInverse:(BOOL)inverse
{
    CAGradientLayer *newShadow = [[[CAGradientLayer alloc] init] autorelease];
    CGRect newShadowFrame =
        CGRectMake(0, 0, self.frame.size.width,
            inverse ? SHADOW_INVERSE_HEIGHT : SHADOW_HEIGHT);
    newShadow.frame = newShadowFrame;
    CGColorRef darkColor =
        [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:
            inverse ? (SHADOW_INVERSE_HEIGHT / SHADOW_HEIGHT) * 0.25 : 0.25].CGColor;
    CGColorRef lightColor =
        [self.backgroundColor colorWithAlphaComponent:0.0].CGColor;
    newShadow.colors =
        [NSArray arrayWithObjects:
            (id)(inverse ? lightColor : darkColor),
            (id)(inverse ? darkColor : lightColor),
        nil];
    return newShadow;
}

后:

- (CAGradientLayer *)shadowAsInverse:(BOOL)inverse
{
    CAGradientLayer *newShadow = [[CAGradientLayer alloc] init];
    CGRect newShadowFrame =
        CGRectMake(0, 0, self.frame.size.width,
            inverse ? SHADOW_INVERSE_HEIGHT : SHADOW_HEIGHT);
    newShadow.frame = newShadowFrame;
    CGColorRef darkColor =
        [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:
            inverse ? (SHADOW_INVERSE_HEIGHT / SHADOW_HEIGHT) * 0.25 : 0.25].CGColor;
    CGColorRef lightColor =
        [self.backgroundColor colorWithAlphaComponent:0.0].CGColor;
    newShadow.colors =
        [NSArray arrayWithObjects:
            (__bridge id)(inverse ? lightColor : darkColor),
            (__bridge id)(inverse ? darkColor : lightColor),
        nil];
    return newShadow;
}

2 个答案:

答案 0 :(得分:8)

Apple的 Transitioning to ARC Release Notes 有一个标题为“编译器处理从Cocoa方法返回的CF对象”的小节,以CAGradientLayer.colors为例。它说这段代码将按原样运行:

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        CAGradientLayer *gradientLayer = (CAGradientLayer *)[self layer];
        gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor darkGrayColor] CGColor],
                                                         (id)[[UIColor lightGrayColor] CGColor], nil];
        gradientLayer.startPoint = CGPointMake(0.0, 0.0);
        gradientLayer.endPoint = CGPointMake(1.0, 1.0);
    }
    return self;
}

所以也许你应该尝试重写这样的代码:

- (CAGradientLayer *)shadowAsInverse:(BOOL)inverse
{
    CAGradientLayer *newShadow = [[CAGradientLayer alloc] init];
    CGRect newShadowFrame =
        CGRectMake(0, 0, self.frame.size.width,
            inverse ? SHADOW_INVERSE_HEIGHT : SHADOW_HEIGHT);
    newShadow.frame = newShadowFrame;
    UIColor darkColor =
        [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:
            inverse ? (SHADOW_INVERSE_HEIGHT / SHADOW_HEIGHT) * 0.25 : 0.25];
    UIColor lightColor =
        [self.backgroundColor colorWithAlphaComponent:0.0];
    newShadow.colors = inverse
        ? [NSArray arrayWithObjects:[lightColor CGColor], [darkColor CGColor], nil]
        : [NSArray arrayWithObjects:[darkColor CGColor], [lightColor CGColor], nil];
    return newShadow;
}

答案 1 :(得分:1)

我用另一种方式解决了这个问题:

在Xcode中,在目标Build Phases选项卡中,打开组以显示Sources Compile源文件列表。双击要在9月份输入标志的文件,在弹出式面板中输入-fno-objc-arc,然后单击“完成”。

这会禁用ARC文件,只会给您带来问题。