UIGraphicsGetCurrentContext问题

时间:2011-03-26 09:22:24

标签: objective-c core-graphics quartz-graphics quartz-2d

我在绘制传递此图形上下文的对象时遇到问题

CGContextRef context = UIGraphicsGetCurrentContext();

这是我用来绘制的代码

- (void) draw: (CGContextRef) gc
{       
    CGFloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f}; // R G B + Alpha
    CGPoint p [2];
    p[0].x = xStart ;
    p[0].y = yStart ;
    p[1].x = xEnd ;
    p[1].y = yEnd ;
    CGContextSetStrokeColor(gc, black); //Definie la couleur
    CGContextStrokeLineSegments(gc, p, 2 ) ;
}

某些对象绘制,其他没有(非常奇怪)并且在控制台中我读到:

    Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextDrawImage: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0

1 个答案:

答案 0 :(得分:1)

所以我无法说出无效的内容错误,因为我们需要查看剩下的代码,但看起来颜色空间可能不正确(默认情况下不是RGB)。在调用stroke之前添加它:

CGContextSetStrokeColorSpace(gc, CGColorSpaceCreateDeviceRGB());