2014-06-14 2 views
1
나는 다음 클래스 사용하여 간단한 획 원을 그리기하고

:아이폰 OS - 코어 그래픽 색상으로 작동하지만, 검정, 흰색과 회색

@implementation StrokedCircle 

- (id)initWithRadius:(CGFloat)radius strokeWidth:(CGFloat)strokeWidth strokeColor:(UIColor *)strokeColor 
{ 
    self = [super initWithRadius:radius]; 
    if (self) 
    { 
     _strokeWidth = strokeWidth; 
     _strokeColor = strokeColor; 
    } 
    return self; 
} 


- (void)drawRect:(CGRect)rect 
{ 
    NSLog(@"Drawing with color %@ and stroke width %f", self.strokeColor, self.strokeWidth); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGRect circleRect = CGRectInset(rect, self.strokeWidth, self.strokeWidth); 
    CGContextAddEllipseInRect(context, circleRect); 
    CGContextSetLineWidth(context, self.strokeWidth); 
    CGContextSetStrokeColor(context, CGColorGetComponents([self.strokeColor CGColor])); 
    CGContextStrokePath(context); 
} 

@end 

참고 : 슈퍼 클래스는 간단한 원입니다 (UIView의 sublass) radius 속성이 설정되어 있고보기의 배경색이 clearColor으로 설정되어 있습니다.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    StrokedCircle *strokedCircle = [[StrokedCircle alloc] initWithRadius:50.0 strokeWidth:1.0 strokeColor:[UIColor blueColor]]; 
    strokedCircle.center = self.view.center; 
    [self.view addSubview:strokedCircle]; 
} 

이 실제로 잘 작동, 콘솔 출력 :보기 컨트롤러에서

, 나는 viewDidLoad에 다음 코드를 추가 2014-06-14 10:31:58.270 ShapeTester[1445:60b] Drawing with color UIDeviceRGBColorSpace 0 0 1 1 and stroke width 1.000000과 파란색 원은 화면의 중앙에 표시됩니다. 내가 [UIUColor blackColor], [UIColor grayColor], 또는 [UIColor whiteColor]에 색상을 수정할 때

blue stroked circle

그러나, 더 원이 더 이상 표시되지 않습니다 (그러나 또한 뷰의 backgroundColor 변화).

누구든지이 동작의 이유를 알고 있습니까? 코어 그래픽이 회색 음영을 그립니 까? 내가 Core Graphics Programming Guide의 적절한 섹션을 통해 읽었지만, 이것에 대해서는 아무 것도 언급되지 않았다.

답변

6

검정색, 흰색 및 회색 (사용자가 명명 한 방법으로 반환 됨)은 RGB 색상 공간이 아닙니다. 그들은 회색 음영 색 공간에 있습니다. 그레이 스케일 색상 공간에는 세 가지 요소 (알파 포함)가 아닌 한 요소 만 있습니다. 따라서 획 색상의 구성 요소 중 하나만 설정하면 나머지 두 구성 요소는 정의되지 않습니다. 이 문제로 인해 알파를 0으로 설정하면 결국 아무것도 얻지 못할 것입니다.

CGContextSetStrokeColor을 사용하지 마십시오. 색상 공간 (CGContextSetStrokeColorSpace을 사용하여 설정해야하는)에 대해 걱정해야합니다. 대신, 색 공간과 색 구성 요소를 모두 설정하는 CGContextSetStrokeColorWithColor을 사용하십시오.

CGContextSetStrokeColorWithColor(context, self.strokeColor.CGColor);