Iphone CGContextFilllellipseInRect:无效的上下文
当我启动我的应用程序时,我不断得到这个cgContextFilllellipseInrect:invalid context错误。我的应用程序只是为了让圆圈变得“可捏” 调试器向我显示以下内容:Iphone CGContextFilllellipseInRect:无效的上下文,iphone,Iphone,当我启动我的应用程序时,我不断得到这个cgContextFilllellipseInrect:invalid context错误。我的应用程序只是为了让圆圈变得“可捏” 调试器向我显示以下内容: [Session started at 2010-05-23 18:21:25 +0800.] 2010-05-23 18:21:27.140 Erase[8045:207] I'm being redrawn. Sun May 23 18:21:27 Sidwyn-Kohs-MacBook-Pro.l
[Session started at 2010-05-23 18:21:25 +0800.]
2010-05-23 18:21:27.140 Erase[8045:207] I'm being redrawn.
Sun May 23 18:21:27 Sidwyn-Kohs-MacBook-Pro.local Erase[8045] <Error>: CGContextFillEllipseInRect: invalid context
2010-05-23 18:21:27.144 Erase[8045:207] New value of counter is 1
2010-05-23 18:21:27.144 Erase[8045:207] I'm being redrawn.
Sun May 23 18:21:27 Sidwyn-Kohs-MacBook-Pro.local Erase[8045] <Error>: CGContextFillEllipseInRect: invalid context
2010-05-23 18:21:27.144 Erase[8045:207] New value of counter is 2
2010-05-23 18:21:27.144 Erase[8045:207] I'm being redrawn.
Sun May 23 18:21:27 Sidwyn-Kohs-MacBook-Pro.local Erase[8045] <Error>: CGContextFillEllipseInRect: invalid context
2010-05-23 18:21:27.144 Erase[8045:207] New value of counter is 3
2010-05-23 18:21:27.145 Erase[8045:207] I'm being redrawn.
Sun May 23 18:21:27 Sidwyn-Kohs-MacBook-Pro.local Erase[8045] <Error>: CGContextFillEllipseInRect: invalid context
2010-05-23 18:21:27.145 Erase[8045:207] New value of counter is 4
2010-05-23 18:21:27.148 Erase[8045:207] I'm being redrawn.
Sun May 23 18:21:27 Sidwyn-Kohs-MacBook-Pro.local Erase[8045] <Error>: CGContextFillEllipseInRect: invalid context
2010-05-23 18:21:27.149 Erase[8045:207] New value of counter is 5
2010-05-23 18:21:27.150 Erase[8045:207] I'm being redrawn.
Sun May 23 18:21:27 Sidwyn-Kohs-MacBook-Pro.local Erase[8045] <Error>: CGContextFillEllipseInRect: invalid context
2010-05-23 18:21:27.150 Erase[8045:207] New value of counter is 6
我有两个问题:
1为什么要更新计数器6次?我删除了行[超级设置需要显示];但它变成了4倍。
2无效上下文错误是什么
谢谢各位
我不确定这六个调用,但至少有两个调用正在发生,因为框架正在调用drawRect:您也是。您不需要自己调用drawRect:因此不需要重写setNeedsDisplay:。
您应该从drawRect内部调用UIGraphicsGetCurrentContext:。不要缓存它。它可能会失败,因为框架不能保证在每次调用时使用相同的上下文,或者因为您直接调用drawRect,或者两者都调用;我不知道是哪个。
你好,马塞洛,谢谢你的回复。通过1删除对setNeedsDisplay的覆盖2将CGContextRef上下文放在drawRect:方法中,完全按照您所说的做了。然而,我仍然接到2个电话,当我启动应用程序时,屏幕上有一个巨大的椭圆形,它与屏幕的宽度和高度相匹配。知道吗?这个巨大的椭圆是由于第二个调用计数器==1,它随后调用第二个椭圆代码并使用框架传递的rect。它基本上是建议你画整个屏幕。您是否曾调用setNeedsDisplay:或setNeedsDisplayInRect:?这可能是这两个电话的原因。
//
// ImageView.m
// Erase
//
#import "ImageView.h"
#import "EraseViewController.h"
@implementation ImageView
-(void)setNewRect:(CGRect)anotherRect{
newRect = anotherRect;
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
newRect = CGRectMake(0,0,0,0);
ref = UIGraphicsGetCurrentContext();
}
return self;
}
- (void)drawRect:(CGRect)rect {
static int counter = 0;
CGRect veryNewRect = CGRectMake(30.0, 210.0, 60.0, 60.0);
NSLog(@"I'm being redrawn.");
if (counter == 0){
CGContextFillEllipseInRect(ref, veryNewRect);
}
else{
CGContextFillEllipseInRect(ref, rect);
}
counter++;
NSLog(@"New value of counter is %d", counter);
}
- (void)setNeedsDisplay{
[super setNeedsDisplay];
[self drawRect:newRect];
}
- (void)dealloc {
[super dealloc];
}
@end