gpt4 book ai didi

iphone - 传递 UIColor 作为参数时崩溃

转载 作者:行者123 更新时间:2023-12-03 20:24:19 25 4
gpt4 key购买 nike

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

当调用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 模拟器中的构建文件和应用程序文件夹。这将使它再次发挥作用。唯一能让它持续工作的事情是通过更改 UIColor *color @property 来分配,反之亦然。这将使我能够重建应用程序并再次毫无问题地运行它一两次。然后它就崩溃了。哦,它在设备上也做同样的事情。有任何想法吗???

提前致谢,

标记

最佳答案

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

@property (nonatomic, retain) UIColor *color;

该行绕过属性 setter 并将自动释放的对象直接分配给成员。

color = [UIColor colorWith...

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

self.color = [UIColor colorWith...

[color retain];

color = [[UIColor alloc] initWith...

关于iphone - 传递 UIColor 作为参数时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3095730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com