【问题标题】:Crashing while passing UIColor as argument将 UIColor 作为参数传递时崩溃
【发布时间】:2010-06-22 17:56:49
【问题描述】:

我有一个小的 UIView 对象,CircleColorView.m,它只是创建一个带有彩色圆圈的视图。然后我将该视图用作一堆按钮(所有不同颜色)的背景。

我的问题发生在 drawRect: 方法被调用时。当我引用 UIColor 的对象颜色时,我会崩溃,但只是有时。

我很困惑。这是我的 UIView:

ColorCircleView.h

#import <UIKit/UIKit.h>
#import "Constants.h"

@interface CircleColorView : UIView {

    UIColor *color;

}

@property (nonatomic, retain) UIColor *color;

- (id)initWithFrame:(CGRect)frame andColor:(UIColor *)circleColor;

@end

这里是 ColorCircleView.m

#import "CircleColorView.h"


@implementation CircleColorView
@synthesize color;

- (id)initWithFrame:(CGRect)frame andColor:(UIColor *)circleColor {
    if ((self = [super initWithFrame:frame])) {
        color = [UIColor colorWithCGColor:[circleColor CGColor]];

                // have also tried
                // color = circleColor;


    }

    return self;
}


- (void) drawRect: (CGRect) aRect
{
    CGFloat iconSize = self.frame.size.width;

        // Create a new path
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGMutablePathRef path = CGPathCreateMutable();

        // Set up fill for circle
    const CGFloat* fill = CGColorGetComponents(color.CGColor);
    CGContextSetFillColor(context, fill);

        // Add circle to path
    CGRect limits = CGRectMake(8.0f, 8.0f, iconSize - 16.0f, iconSize - 16.0f);
    CGPathAddEllipseInRect(path, NULL, limits);
    CGContextAddPath(context, path);

    CGContextFillEllipseInRect(context, limits);
    CGContextFillPath(context);
    CFRelease(path);
}




- (void)dealloc {
    [color release];
    [super dealloc];
}


@end

这是我用来创建 CircleColorView 并将其添加到按钮图像的代码。它位于一个循环中,该循环正在遍历一个字符串数组,其中颜色值由 ;

分隔
NSArray *values = [[NSArray alloc] initWithArray:[[[colorListArray objectAtIndex:i] objectAtIndex:1] componentsSeparatedByString:@";"]];
    float red = [[values objectAtIndex:0] floatValue];
    float green = [[values objectAtIndex:1] floatValue];
    float blue = [[values objectAtIndex:2] floatValue];

    UIColor *color = [[UIColor alloc]
                      initWithRed: (float) (red/255.0f)
                      green: (float) (green/255.0f)
                      blue:  (float) (blue/255.0f)
                      alpha: 1.0];
    UIButton *newColorButton = [UIButton buttonWithType:0];

        //Create Colored Circle
    CircleColorView *circle = [[CircleColorView alloc] initWithFrame:CGRectMake(0, 0, 75, 75) andColor:color ];
    circle.backgroundColor = [UIColor clearColor];

       //Set Button Attributes
    [newColorButton setTitle:[[colorListArray objectAtIndex:i] objectAtIndex:1] forState:UIControlStateDisabled];
    [newColorButton setFrame:CGRectMake(600+(i*82), 12, 75, 75)]; //set location of each button in scrollview
    [newColorButton addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchDown];
    [newColorButton setTag:tagNum];
    [barContentView addSubview:newColorButton];
    [circle release];
    [color release];
    [values release];

我已经记录下来看看会发生什么。看起来它运行 CircleColorView 的 initWithFrame:andColor: 就好了。然后当 drawRect: 被调用时,它会在第一次引用属性 'color' 时崩溃。即使它只是要求它提供类似 [颜色描述] 的描述。

任何想法。我创建这个 UIColor *color 错误吗?还是错误地保留它?这是另一个奇怪的事情。这段代码可以正常运行一段时间。然后当我退出并重新启动应用程序时,它会崩溃。为了让它再次工作,我删除了 iPhone 模拟器中的构建文件和 app 文件夹。这将允许它再次工作。唯一能让它持续工作的另一件事是只需将 UIColor *color @property 更改为 assign ,反之亦然。这将允许我重新构建应用程序并再次运行它而没有问题。然后它崩溃了。哦,它在设备上做同样的事情。有任何想法吗???

提前致谢,

标记

【问题讨论】:

  • 您的问题解决了吗?请将答案标记为已接受。

标签: iphone uiview uicolor


【解决方案1】:

这一行声明了颜色属性,分配时会保留颜色。

@property (nonatomic, retain) UIColor *color;

这一行绕过了属性设置器并直接将一个自动释放的对象分配给成员。

color = [UIColor colorWith...

您可以使用以下任一方法修复它:

self.color = [UIColor colorWith...

[color retain];

color = [[UIColor alloc] initWith...

【讨论】:

    【解决方案2】:

    因为您直接分配给“颜色”,而不是使用“。”访问器语法,setter 方法没有被调用,所以你分配的值不会被保留。

    您需要明确保留color,或使用 .语法:

    self.color = [UIColor colorWithCGColor:[circleColor CGColor]];
    

    【讨论】:

      【解决方案3】:
      color = [UIColor colorWithCGColor:[circleColor CGColor]];
      [color retain];
      

      由于没有[color retain] 代码,它会自动发布,并且会随机发生。

      如果您做了类似color = [[UIColor alloc] initWithCGColor:[circleColor CGColor]]; 或在创建中具有init 的类似操作,您将不需要[color retain]

      【讨论】:

      • 您也不需要将保留分配给 self.color 而不是 color (因为合成属性负责保留),但我记得看到 Apple 建议不要使用 self初始化程序中的 .property (虽然我不太明白为什么)。
      • 是的,我记得看到建议不要使用 self.property。我记得我曾经明白为什么从前,但现在它逃脱了我。
      • 好的,谢谢。它确实解决了这个问题。我在想,由于@property,它会在分配后保留该属性。谢谢。
      • 如果此问题已解决,请确保您选择一个答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多