Ios7 从drawRect调用的方法中存在无效的CGContext错误
我正在尝试编写一个UIView drawRect:方法,该方法调用一个助手方法,用我传递给它的一些参数绘制渐变填充:Ios7 从drawRect调用的方法中存在无效的CGContext错误,ios7,uiview,xcode5,core-graphics,cgcontextref,Ios7,Uiview,Xcode5,Core Graphics,Cgcontextref,我正在尝试编写一个UIView drawRect:方法,该方法调用一个助手方法,用我传递给它的一些参数绘制渐变填充: -(void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); /* code to set up color array, position point for gradient drawing function goes here.... */
-(void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
/* code to set up color array, position point
for gradient drawing function goes here.... */
[self drawGradientWithContext:ctx atPoint:centerPoint withColors:colors];
CGContextRelease(ctx);
}
-(void)drawGradientWithContext:(CGContextRef)ctx atPoint:(CGPoint)centerPoint withColors:(NSArray *)colors {
/*code to set up color spaces and gradient refs
goes here...... */
CGContextDrawRadialGradient(ctx, gradientRef, centerPoint, 0, centerPoint, radius, kCGGradientDrawsBeforeStartLocation);
// release colorspace and gradient refs here....
}
当我在模拟器中运行此代码时,它似乎工作正常(所有渲染都正常),但我收到以下错误消息:
错误:CGContextResetState:无效的上下文0x8dcc190。这是一个严重的错误。
此应用程序或其使用的库正在使用无效的上下文,因此
导致系统稳定性和可靠性的全面降低。这
通知是一种礼貌:请解决此问题。这将成为一个致命的错误
即将发布的更新
收到此错误消息后,我发现CGContextRefs无法作为函数参数传递,因此我尝试了以下方法:
-(void)drawRect:(CGRect)rect {
// code to set up color array, position point
// for gradient drawing function goes here....
[self drawGradientAtPoint:centerPoint withColors:colors];
CGContextRelease(ctx);
}
-(void)drawGradientAtPoint:(CGPoint)centerPoint withColors:(NSArray *)colors {
CGContextRef ctx = UIGraphicsGetCurrentContext();
//code to set up color spaces and gradient refs
//goes here......
CGContextDrawRadialGradient(ctx, gradientRef, centerPoint, 0, centerPoint, radius, kCGGradientDrawsBeforeStartLocation);
// release colorspace and gradient refs
CGContextRelease(ctx);
}
但同样的事情也发生了:程序运行正常,但运行时抱怨我使用了无效的图形上下文
是否有正确的方法将绘图函数委托给助手函数,或者我必须将所有绘图代码塞进drawRect函数?不要调用
CGContextRelease(ctx)
。您没有创建或保留上下文,因此发布上下文不是您的责任
第一次这样做时,会导致上下文过早释放。稍后,当您(或UIKit的其余部分)尝试使用该上下文时,该上下文无效并生成该错误消息。现在它可能不会崩溃,但你只是运气好而已