Iphone 传递UIColor预设值时出现问题

Iphone 传递UIColor预设值时出现问题,iphone,objective-c,uibutton,uicolor,Iphone,Objective C,Uibutton,Uicolor,我有以下组件-ColorButton,它表示基本上是彩色矩形的单个按钮,PaletteView,它是ColorButton对象的网格 代码如下所示: ColorButton.h @interface ColorButton : UIButton { UIColor* color; } -(id) initWithFrame:(CGRect)frame andColor:(UIColor*)color; @property (nonatomic, retain) UIColor* co

我有以下组件-ColorButton,它表示基本上是彩色矩形的单个按钮,PaletteView,它是ColorButton对象的网格

代码如下所示:

ColorButton.h

@interface ColorButton : UIButton {
    UIColor* color;
}

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

@property (nonatomic, retain) UIColor* color;

@end
ColorButton.m

@implementation ColorButton

@synthesize color;

- (id)initWithFrame:(CGRect)frame andColor:(UIColor*)aColor{    
    self = [super initWithFrame:frame];
    if (self) {
        self.color = aColor;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    const float* colors = CGColorGetComponents(color.CGColor);
    CGContextSetRGBFillColor(context, colors[0], colors[1], colors[2], colors[3]);
    CGContextFillRect(context, rect);
}
- (void) initPalette {        
    ColorButton* cb = [[ColorButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30) andColor:[UIColor grayColor]];
    [self addSubview:cb];    
}
- (void) initPalette {    
    UIColor *color = [[UIColor alloc]
                      initWithRed: (float) (100/255.0f)
                      green: (float) (100/255.0f)
                      blue:  (float) (1/255.0f)
                      alpha: 1.0];

    ColorButton* cb = [[ColorButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30) andColor:color];
    [self addSubview:cb];
}
- (id)initWithFrame:(CGRect)frame andColor:(UIColor*)aColor{    
    self = [super initWithFrame:frame];
    if (self) {
        //self.color = aColor;
        self.color = [UIColor redColor];
    }
    return self;
}
PaletteView.m

@implementation ColorButton

@synthesize color;

- (id)initWithFrame:(CGRect)frame andColor:(UIColor*)aColor{    
    self = [super initWithFrame:frame];
    if (self) {
        self.color = aColor;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    const float* colors = CGColorGetComponents(color.CGColor);
    CGContextSetRGBFillColor(context, colors[0], colors[1], colors[2], colors[3]);
    CGContextFillRect(context, rect);
}
- (void) initPalette {        
    ColorButton* cb = [[ColorButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30) andColor:[UIColor grayColor]];
    [self addSubview:cb];    
}
- (void) initPalette {    
    UIColor *color = [[UIColor alloc]
                      initWithRed: (float) (100/255.0f)
                      green: (float) (100/255.0f)
                      blue:  (float) (1/255.0f)
                      alpha: 1.0];

    ColorButton* cb = [[ColorButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30) andColor:color];
    [self addSubview:cb];
}
- (id)initWithFrame:(CGRect)frame andColor:(UIColor*)aColor{    
    self = [super initWithFrame:frame];
    if (self) {
        //self.color = aColor;
        self.color = [UIColor redColor];
    }
    return self;
}
问题是它不起作用——什么都看不见。但是,下面的代码是有效的

PaletteView.m

@implementation ColorButton

@synthesize color;

- (id)initWithFrame:(CGRect)frame andColor:(UIColor*)aColor{    
    self = [super initWithFrame:frame];
    if (self) {
        self.color = aColor;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    const float* colors = CGColorGetComponents(color.CGColor);
    CGContextSetRGBFillColor(context, colors[0], colors[1], colors[2], colors[3]);
    CGContextFillRect(context, rect);
}
- (void) initPalette {        
    ColorButton* cb = [[ColorButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30) andColor:[UIColor grayColor]];
    [self addSubview:cb];    
}
- (void) initPalette {    
    UIColor *color = [[UIColor alloc]
                      initWithRed: (float) (100/255.0f)
                      green: (float) (100/255.0f)
                      blue:  (float) (1/255.0f)
                      alpha: 1.0];

    ColorButton* cb = [[ColorButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30) andColor:color];
    [self addSubview:cb];
}
- (id)initWithFrame:(CGRect)frame andColor:(UIColor*)aColor{    
    self = [super initWithFrame:frame];
    if (self) {
        //self.color = aColor;
        self.color = [UIColor redColor];
    }
    return self;
}
在本例中,与[UIColor grayColor]-自动释放对象相比,我传递了未自动释放的UIColor对象

以下代码也适用:

ColorButton.m

@implementation ColorButton

@synthesize color;

- (id)initWithFrame:(CGRect)frame andColor:(UIColor*)aColor{    
    self = [super initWithFrame:frame];
    if (self) {
        self.color = aColor;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    const float* colors = CGColorGetComponents(color.CGColor);
    CGContextSetRGBFillColor(context, colors[0], colors[1], colors[2], colors[3]);
    CGContextFillRect(context, rect);
}
- (void) initPalette {        
    ColorButton* cb = [[ColorButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30) andColor:[UIColor grayColor]];
    [self addSubview:cb];    
}
- (void) initPalette {    
    UIColor *color = [[UIColor alloc]
                      initWithRed: (float) (100/255.0f)
                      green: (float) (100/255.0f)
                      blue:  (float) (1/255.0f)
                      alpha: 1.0];

    ColorButton* cb = [[ColorButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30) andColor:color];
    [self addSubview:cb];
}
- (id)initWithFrame:(CGRect)frame andColor:(UIColor*)aColor{    
    self = [super initWithFrame:frame];
    if (self) {
        //self.color = aColor;
        self.color = [UIColor redColor];
    }
    return self;
}
有人能解释一下这里发生了什么,为什么我不能传递像[UIColor grayColor]这样的对象吗?解决我的任务的正确方法是什么?将颜色值从PaletteView传递到ColorButton


谢谢

问题在于,您正在使用
CGColorGetComponents
为CGColor请求颜色组件。此方法可能返回不同数量的组件,具体取决于基础颜色对象的颜色空间。例如,
[UIColor grayColor]
可能位于灰度颜色空间中,因此仅设置颜色[0]


如果要为上下文设置填充颜色,可以使用直接获取CGColorRef对象的
CGContextSetFillColorWithColor
,因此根本不需要使用组件。

Bingo!这个API有效!谢谢你给我指点颜色空间的东西。