4
내가 레이어를 사용하는 응용 프로그램을 구축하기 위해 노력하고있어
는 내 응용 프로그램 구조는 내가 LayersView에 여러 레이어를 추가 할사용자의 CALayer - 유효하지 않은 컨텍스트 0x0으로
UIView --> UIScrollView --> UIView --> LayersView (Custom UIView)
--> UIImageView
, 그래서 정의를 구축했습니다 CALayer
UIBezierPath
을 사용하여 점 집합을 그립니다.
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
내 LayrsView에서 새 레이어를 시작하고있어#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
,이 코드를 사용하고 있습니다 :
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];
}
을 문제는 앱 점심 식사 때마다 다음과 같은 오류가 발생합니다.
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
을 시도하고 그것은 나에게주는이 : 문제는 어디
2012-09-13 08:05:47.650 abcd[20744:707] <CGContext 0x15d950>
그래서, 왜 내가 왜이 할 수있는 문맥이 무효입니다 내 맞춤 설정에 그리지 마십시오 CALayer
?
안녕하세요 흉터 - 답변을 게시 해 주셔서 감사합니다. 시간을 절약 해주세요. 추가 할 것 중 한 가지만 추가하면됩니다. 예를 들어 푸시 할 경우 즉, 드로잉을 완료하면 기존 컨텍스트를 동일한 상태로 두어야합니다. 즉, 컨텍스트를 푸시하는 모든 곳에서 UIGraphicsPopContext()를 호출해야합니다. 그리기를 끝내면 –