【发布时间】:2026-01-15 08:35:01
【问题描述】:
我的应用在 CGContext.drawPDFPage 上随机崩溃,但无法始终如一地重现该错误。它主要发生在 iPad 上。代码如下所示:
override func draw(_ layer: CALayer, in ctx: CGContext) {
UIGraphicsPushContext(ctx)
ctx.saveGState()
defer {
ctx.restoreGState()
UIGraphicsPopContext()
}
ctx.setFillColor(UIColor.white.cgColor)
ctx.fill(viewBounds)
ctx.translateBy(x: 0, y: viewBounds.height)
ctx.scaleBy(x: 1, y: -1)
ctx.scaleBy(x: pdfScale, y: pdfScale)
ctx.interpolationQuality = .high
ctx.setRenderingIntent(.defaultIntent)
ctx.beginPDFPage(nil)
ctx.drawPDFPage(pdfPage) // This is where the crash happens
ctx.endPDFPage()
}
堆栈跟踪以CGPDFObjectGetReference 结尾,如下所示:
错误是:EXC_BAD_ACCESS。如果您发现任何可能导致此崩溃的原因,将对我有很大帮助!
【问题讨论】:
-
发生这种情况时,您的控制台是否有错误?会不会是内存问题?由于您绘制“更大的图像”,这可能是它在 iPad 上而不是在 iPhone 上崩溃的原因。还有
ctx.drawPDFPage(pdfPage),pdfPage定义在哪里? -
@Larme 错误是“EXC_BAD_ACCESS”,没有更多信息。这就是为什么调试如此困难的原因。内存似乎不够高导致崩溃... pdfPage 是在同一个类中定义的。