CGContextStrokePath(上下文)崩溃。为什么?

时间:2012-06-02 17:19:24

标签: objective-c ios quartz-2d cgcontext

我是Quartz 2D和环境世界的新手。为什么这段代码会崩溃?

NSMutableData* pdfData = [[NSMutableData alloc] initWithLength:1000];

CGRect bounds = (CGRectMake(100, 100, 100, 100));
NSDictionary* documentInfo = nil;

UIGraphicsBeginPDFContextToData (pdfData,
                                 bounds,
                                 documentInfo);

CGContextRef context = UIGraphicsGetCurrentContext();
NSLog (@"context: %@", context);

// Drawing lines with a white stroke color
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(context, 2.0);

// Draw a single line from left to right
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGContextStrokePath(context);

[pdfData release];

代码在CGContextStrokePath(上下文)崩溃时出现EXC_BAD_ACCESS

谢谢。

1 个答案:

答案 0 :(得分:1)

在绘制PDF图形上下文之前,您需要创建一个包含UIGraphicsBeginPDFPageUIGraphicsBeginPDFPageWithInfo的网页。

CGRect bounds = CGRectMake(0, 0, 100, 100);
UIGraphicsBeginPDFContextToData(pdfData, bounds, nil);
UIGraphicsBeginPDFPage(bounds, nil);