自定义CALayer - 无效的上下文0x0 [英] Custom CALayer - invalid context 0x0

查看:124
本文介绍了自定义CALayer - 无效的上下文0x0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个使用图层的应用,我的应用结构是

I'm trying to build an app that use layers, my app structure is

UIView --> UIScrollView --> UIView --> LayersView (Custom UIView)
                                   --> UIImageView

我想在LayersView中添加多个图层,所以我构建了一个自定义 CALayer 使用 UIBezierPath 绘制一组积分。

I want to add multiple layers to the LayersView, so I've build a custom CALayer that use UIBezierPath to draw a set of points.

CALayerBezierPath .h

CALayerBezierPath.h

#import <QuartzCore/QuartzCore.h>

@interface CALayerBezierPath : CALayer {
    NSMutableArray *pointsArray;
}
@property (nonatomic, retain) NSMutableArray *pointsArray;

- (void) initVariables;
- (void) addNewPoints:(CGPoint)newPoint;
@end

CALayerBezierPath.m

CALayerBezierPath.m

#import "CALayerBezierPath.h"

@implementation CALayerBezierPath

@dynamic pointsArray;

-(void)drawInContext:(CGContextRef)ctx {
    NSLog(@"CALayerBezierPath - drawInContext");
    if ([pointsArray count] > 0) {
        UIColor *color = [UIColor redColor];
        [color set];
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:[[pointsArray objectAtIndex:0] CGPointValue]];
        for (int x = 1; x < [pointsArray count]; x++) {
            [path addLineToPoint:[[pointsArray objectAtIndex:x] CGPointValue]];
        }
        [path closePath]; // Implicitly does a line between p4 and p1
        [path fill]; // If you want it filled, or...
        [path stroke]; // ...if you want to draw the outline.
    }
}

-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    NSLog(@"drawLayer");// It's never called, i don't know why
}

- (void) initVariables {
    pointsArray = [[NSMutableArray alloc] init];
}

- (void) addNewPoints:(CGPoint)newPoint {
    [pointsArray addObject:[NSValue valueWithCGPoint:newPoint]];
}

@end

当我开始时我的LayrsView中的新图层,我正在使用此代码:

When I'm initiating a new layer in my LayrsView, I'm using this code:

    self.layer.backgroundColor = [UIColor whiteColor].CGColor;
    self.layer.cornerRadius = 20.0;
    self.layer.frame = CGRectInset(self.layer.frame, 0, 0);

    for (PlansClass *pclass in layersContent) {
        CALayerBezierPath *sublayer = [CALayerBezierPath layer];
        [sublayer initVariables];

        NSDictionary* json = [pclass.listOfPoints objectFromJSONString];

        float largerX = 0; float largerY = 0;
        float smallerX = 10000; float smallerY = 10000;

        for (NSDictionary *dic in json) {

            [sublayer addNewPoints:CGPointMake([[[json objectForKey:dic] objectForKey:@"x"] floatValue], [[[json objectForKey:dic] objectForKey:@"y"] floatValue])];

            if (largerX < [[[json objectForKey:dic] objectForKey:@"x"] floatValue]) {
                largerX = [[[json objectForKey:dic] objectForKey:@"x"] floatValue];
            }

            if (smallerX > [[[json objectForKey:dic] objectForKey:@"x"] floatValue]) {
                smallerX = [[[json objectForKey:dic] objectForKey:@"x"] floatValue];
            }

            if (largerY < [[[json objectForKey:dic] objectForKey:@"y"] floatValue]) {
                largerY = [[[json objectForKey:dic] objectForKey:@"y"] floatValue];
            }

            if (smallerY > [[[json objectForKey:dic] objectForKey:@"y"] floatValue]) {
                smallerY = [[[json objectForKey:dic] objectForKey:@"y"] floatValue];
            }
        }
        sublayer.frame = CGRectMake(smallerX, smallerY, largerX - smallerX, largerY - smallerY);
        sublayer.backgroundColor = [UIColor redColor].CGColor;
        [self.layer addSublayer:sublayer];
        [sublayer setNeedsDisplay];
    }

问题是应用程序午餐时,它给我这个错误:

The problem is whenever the app lunch, it's give me this error:

2012-09-13 08:05:47.648 abcd[20744:707] CALayerBezierPath - drawInContext
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSaveGState: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetFlatness: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextAddPath: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextDrawPath: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextRestoreGState: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSaveGState: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetLineWidth: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetLineJoin: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetLineCap: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetMiterLimit: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetFlatness: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextAddPath: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextDrawPath: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextRestoreGState: invalid context 0x0

另外,我在 drawInContext中试图 NSLog ctx / code>方法,它给了我这个:

Also, I've tried to NSLog the ctx in the drawInContext methods and it gives me this:

2012-09-13 08:05:47.650 abcd[20744:707] <CGContext 0x15d950>

那么,问题出在哪里,为什么上下文无效,为什么我无法吸引我的自定义 CALayer

So, Where is the problem, why is the context is invalid, why i can't draw in my custom CALayer?

推荐答案

问题是我没做过推动上下文 ctx ,我试图推动&流行。所以解决方案就像把这一行放在 drawInContext中的if语句一样简单

The problem is I've didn't push the context ctx, I was trying push & pop. so the solution is as simple as putting this line in the if statement in drawInContext

UIGraphicsPushContext(ctx);

一切正常。

这篇关于自定义CALayer - 无效的上下文0x0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆