0

我是 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(context) 与 EXC_BAD_ACCESS 崩溃

谢谢你。

4

1 回答 1

2

在绘制到 PDF 图形上下文之前,您需要使用 UIGraphicsBeginPDFPage或 创建一个页面UIGraphicsBeginPDFPageWithInfo

CGRect bounds = CGRectMake(0, 0, 100, 100);
UIGraphicsBeginPDFContextToData(pdfData, bounds, nil);
UIGraphicsBeginPDFPage(bounds, nil);
于 2012-06-02T17:32:29.143 回答