ios - cgcontext严重错误: draw bezierPath in initwithcoder?

标签 ios objective-c core-graphics cgcontext uibezierpath

我想加载一个包含 uibezierPath 的类的实例,并在应用程序启动时重绘这些先前的路径。字典返回正确的实例,但我无法绘制路径: View 是在 Storyboard中创建的,所以我使用了 initWithCoder,如果我使用 viewDidLoad,则不会调用此方法。错误是:

previousArrays : ( { firstPath = ""; }, { firstPath = ""; }, { firstPath = ""; }, { firstPath = ""; } ) Dec 30 17:02:36 iPhone MyProject[1818] : CGContextAddPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

这是我的代码:(触摸后,路径被保存,然后再次启动应用程序时,出现错误。但是当我绘制时,没有问题。绘制路径有效。它是在返回时app,并在initWithCoder中绘制,问题就出现了。)

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithCGPath:myPath];//nscoding compliant
    DataForPath *firstPath = [[DataForPath alloc] init];
    firstPath.path = bezierPath;
    firstPath.colorInArray = @(currentColor);
    NSDictionary *dict = @{@"firstPath":firstPath};

    [SaveData saveData:dict];
}


-(id)initWithCoder:(NSCoder *)aDecoder{
    if ( !(self=[super initWithCoder:aDecoder])) return nil;
    //...
    myPath = CGPathCreateMutable();
    CGContextRef context = UIGraphicsGetCurrentContext();
    NSArray *previousArrays = [SaveData loadData];
    //NSLog("previousArrays : %@", previousArrays )...
    for ( NSDictionary*dict in previousArrays){
        UIBezierPath *eachPath = dict[@"path"];
        int color = [dict[@"colorInArray"] intValue];
        UIColor *objectColor = [self.possibleColor objectAtIndex:color];

        CGContextAddPath(context, eachPath.CGPath);
        CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
        CGContextDrawPath(context, kCGPathStroke);

        /*
        CGContextSetStrokeColorWithColor(context, objectColor.CGColor);
        CGContextSaveGState(context);
        [eachPath stroke];
        CGContextRestoreGState(context);
        */
    }
    return self;
}

编辑:for 循环中的先前路径未绘制?

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    if ( firstLaunchWithPreviousPaths ){
        for ( NSDictionary*dict in previousArrays){
            NSLog(@"firstLaunch"); //is called
            UIBezierPath *eachPath = dict[@"path"];

            CGContextAddPath(context, eachPath.CGPath);
            CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
            CGContextDrawPath(context, kCGPathStroke);
            //nothing is drawn?
        }
    }
    //with touchesEnd : this works
    /*
    CGContextAddPath(context, myPath);
    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
    CGContextDrawPath(context, kCGPathStroke);
     */
}

最佳答案

错误消息告诉您问题所在。你是说:

CGContextRef context = UIGraphicsGetCurrentContext();

但是 initWithCoder:没有当前上下文。

只能在有图形上下文的地方进行绘制。要么制作一个,要么将您的代码移动到有一个的地方(例如 drawRect:)。

或者,如果您正在尝试构造一个可变的 CGPath,请不要引用任何图形上下文:使用路径,而不是上下文。 CGMutablePath 有一整套自己的函数来构造路径。但是当然你不能抚摸或绘制它 - 它只是一个路径。当您拥有图形上下文时,您将能够在稍后对其进行描边或绘制;您为上下文提供路径,现在您可以抚摸或绘制它。描边和绘图只能在图形上下文中发生。而你这里没有。

关于ios - cgcontext严重错误: draw bezierPath in initwithcoder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27708683/

相关文章:

iphone - iOS iPhone 5 选择正确的 Storyboard

ios - Objective C 应用程序中的 “Swift Language Version” 错误

objective-c - 当我设置 anchor 时,我的图像没有加载

ios - 从圆形或 donut 中绘制线段

android - 应用程序中 bundle 的 API key 和 secret 的最佳实践

ios - 刷新 TableView iphone

ios - 状态栏方向值为 __C.UIInterfaceOrientation

iphone - 使 NSObject 的自定义类可序列化

iphone - 应用程序文档目录的物理路径

iphone - 在 iPhone 上创建舷窗动画过渡?