2015-06-03 27 views
0

我是新來的objective-c,現在我遇到了一個我不明白如何解決的問題。我已閱讀此主題CGContextSetFillColorWithColor: invalid context 0x0,但未找到答案。<Error>:CGContextSetFillColorWithColor:無效的上下文0x0

的問題是,我得到這樣一個錯誤2:

CGContextSetFillColorWithColor:無效的上下文爲0x0。這是一個嚴重的錯誤。此應用程序或其使用的庫正在使用無效的上下文,從而導致系統穩定性和可靠性的整體降級。這個通知是禮貌的:請解決這個問題。這將成爲即將到來的更新中的致命錯誤。

這是我的代碼:

#import "PushButtonView.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation PushButtonView 

@synthesize isAddButton; 
@synthesize fillColor; 

-(id)initWithCoder:(NSCoder *)aDecoder{ 
    self = [super initWithCoder:(NSCoder *)aDecoder]; 
    if (!self) { 
    return nil; 
} 
[self inspectableDefaults]; 
return self; 
} 

-(void)inspectableDefaults{ 
    self.isAddButton = YES; 
    self.fillColor = [UIColor colorWithRed:0.95 green:0.6 blue:0.5 alpha:1];  
    [self.fillColor setFill]; 
} 

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextClearRect(context, rect); 
    if (self.isAddButton) { 
     self.fillColor = [UIColor colorWithRed:0.3 green:0.85 blue:0.91 alpha:1]; 
    } else 
    self.fillColor = [UIColor colorWithRed:0.95 green:0.6 blue:0.5 alpha:1]; 
    [self.fillColor setFill]; 
    CGContextFillEllipseInRect(context, rect); 

//Line characteristics 
    CGContextSetRGBStrokeColor(context, 1, 1, 1, 1); 
    CGContextSetLineWidth(context, 4); 

// Horizontal line 
    CGPoint bezierStart = {self.bounds.origin.x + (self.bounds.size.width)/5, self.bounds.size.height/2}; 
    CGPoint bezierEnd = {self.bounds.size.width - (self.bounds.size.width)/5, self.bounds.size.height/2}; 
    CGContextBeginPath(context); 
    CGContextMoveToPoint(context, bezierStart.x, bezierStart.y); 
    CGContextAddLineToPoint(context, bezierEnd.x, bezierEnd.y); 
    CGContextStrokePath(context); 

// Vertical line 
    if (self.isAddButton) {      
     CGPoint bezier2Start = {self.bounds.size.width/2, self.bounds.size.width - (self.bounds.size.height)/5}; 
     CGPoint bezier2End = {self.bounds.size.width/2, (self.bounds.origin.y + self.bounds.size.height/5)}; 
     CGContextBeginPath(context); 
     CGContextMoveToPoint(context, bezier2Start.x, bezier2Start.y); 
     CGContextAddLineToPoint(context, bezier2End.x, bezier2End.y); 
     CGContextStrokePath(context); 
    } 

    [self.fillColor setFill]; 
} 
@end 
+0

如果您註釋掉'-setFill'調用,會發生什麼情況? –

+0

如果我註釋掉所有setFill調用,我會得到無色的圖像,但我真的不需要它在inspectableDefaults方法 – AOY

回答

4

在你inspectableDefaults方法,你不應該叫[self.fillColor setFill]setFill方法設置當前圖形上下文的填充顏色,並且由於您沒有任何上下文,因此會出現此錯誤。只要刪除這條線,你很好。

+0

謝謝))你幫了我太多了! – AOY

相關問題