【问题标题】:CGContextStrokePath(context) crashes. Why?CGContextStrokePath(context) 崩溃。为什么?
【发布时间】:2012-06-02 17:19:23
【问题描述】:

我是 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 崩溃

谢谢。

【问题讨论】:

  • 编辑您的问题以粘贴到堆栈跟踪中。

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


【解决方案1】:

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

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

【讨论】:

  • 确实如此。谢谢你。您知道 nsmutabledata* PdfData 是否保留在上下文中吗?或者这是我需要单独保留的东西。 (我需要一个由不同类一一写入的上下文,然后用生成的 pdf 数据将其关闭)
  • 如果 Apple 遵守规则,它会一直保留,直到您调用 UIGraphicsEndPDFContext,这是必需的,目前在您的代码中缺失。我很确定他们会遵守规则。
  • 谢谢!你知道如何在 UIGraphicsEndPDFContext 之后(或之前?)访问 pdfData。当我完成对上下文的绘制后,我需要将它发送回调用者(因此可以从中创建一个 pdf 文件......或者只是重新用作原始 pdf 数据)
猜你喜欢
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多