英文:
How do I avoid crash on PDFPage(image: image) with reason EXC_BAD_ACCESS (KERN_INVALID_ADDRESS) on iOS 16+
问题
The code you provided is encountering a crash on iOS versions higher than 16. The crash occurs on the following line:
if let errorView = getErrorImageView(),
let page = PDFPage(image: errorView) { // <- Crash Observed on this line.
// ...
}
The crash reason is Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
, and it seems to be related to PDF rendering. The provided crash stack trace indicates the issue may be in the PDF rendering code.
You mentioned that this code works on iOS versions below 16 and crashes on higher versions. You may want to investigate any changes or deprecations related to PDF rendering or image handling in the iOS SDK for the higher versions.
If you need further assistance or more specific guidance on resolving this issue, please let me know.
英文:
Same code is working fine on below iOS 16, but crash was observed on higher versions.
Line of Code :
if let errorView = getErrorImageView(),
let page = PDFPage(image: errorView) { // <- Crash Observed on this line.
page.addAnnotation(getTitlePDFAnnotation())
let count = pdfDocument?.pageCount ?? 0
pdfDocument?.insert(page, at: count)
}
fun getErrorImageView() -> UIImage? {
let errorView = UIView()
errorView.bounds = view.bounds
errorView.backgroundColor = .athenaWhite
let errorTitleLabel = UILabel()
errorTitleLabel.translatesAutoresizingMaskIntoConstraints = false
errorTitleLabel.text = "Error"
errorTitleLabel.textAlignment = .center
errorView.addSubview(errorTitleLabel)
// .... added constraint to view
let renderer = UIGraphicsImageRenderer(size: view.bounds.size)
let image = renderer.image { _ in
errorView.drawHierarchy(in: errorView.bounds, afterScreenUpdates: true)
}
return image
}
- Tried same code on different project, and it's working fine, but it's crashing on client project.
- Image is not nil
- Tried with image from xcasset
- Tried with removing alpha
- Tried with enabling zombie object
Crash Reason :
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
#0 0x000000012084d25f in CGBlitInternal::cgsBufferIsConstantValue_internal::TestLimits<unsigned char, 64ul>::operator()(unsigned char const*, unsigned long, CGBlitInternal::cgsBufferIsConstantValue_internal::Limits const&) ()
#1 0x0000000120b89921 in bool CGBlitInternal::cgsBufferIsConstantValue_internal::TestLimitsRow<unsigned char>(void const*, unsigned long, CGBlitInternal::cgsBufferIsConstantValue_internal::Limits const&) (.10518) ()
#2 0x0000000120b8969d in bool CGBlitInternal::CGBufIsConstantValue<(CGBlitVariant)0>(CGBuf const*, float const*, float const*) ()
#3 0x0000000120ba7fe4 in is_image_alpha_opaque ()
#4 0x0000000120ba7bb8 in create_jpeg_copy ()
#5 0x0000000120bbd1df in pdf_DrawImage ()
#6 0x0000000120ad32b8 in CGContextDrawImageWithOptions ()
#7 0x00000001207e2ad2 in CGPDFPageCreateWithImage ()
#8 0x000000012b9c9e44 in +[PDFPage _createPageRefFromImage:andOptions:] ()
#9 0x000000012b9c38dd in -[PDFPage initWithCGImage:options:] ()
#10 0x000000012b9c476f in -[PDFPage initWithImage:options:] ()
#11 0x000000012b9c45ae in -[PDFPage initWithImage:] ()
#12 0x000000010502f913 in @nonobjc PDFPage.init(image:) ()
#13 0x0000000105025232 in PDFPage.__allocating_init(image:) ()
Most of similar code base : https://github.com/ShashikantBhadke/PDFKit_Crash_CodeBase
答案1
得分: 1
更新您的getErrorImageView()
方法如下所示:
func getErrorImageView() -> UIImage? {
let errorView = UIView()
errorView.bounds = view.bounds
errorView.backgroundColor = .white
let errorTitleLabel = UILabel()
errorTitleLabel.translatesAutoresizingMaskIntoConstraints = false
errorTitleLabel.text = "错误"
errorTitleLabel.textAlignment = .center
errorView.addSubview(errorTitleLabel)
// .... 添加视图的约束
// 创建一个UIGraphicsImageRendererFormat并将其opaque属性设置为true
let rendererFormat = UIGraphicsImageRendererFormat.default()
rendererFormat.opaque = true
// 现在将此rendererFormat传递给UIGraphicsImageRenderer
let renderer = UIGraphicsImageRenderer(size: view.bounds.size, format: rendererFormat)
let image = renderer.image { _ in
errorView.drawHierarchy(in: errorView.bounds, afterScreenUpdates: true)
}
return image
}
由于您的崩溃日志建议在创建PDFPage
时检查是否为is_image_alpha_opaque
,而所提供的图像不是不透明的,因此会导致崩溃。现在,由于UIGraphicsImageRenderer
现在将UIGraphicsImageRendererFormat
作为输入参数,我们可以利用它的drawHierarchy
能力,其中我们将其opaque
属性设置为true并且不更新任何alpha
值。
希望这样可以避免崩溃。Happy Coding! ✌️
英文:
Update your getErrorImageView() method with below code
fun getErrorImageView() -> UIImage? {
let errorView = UIView()
errorView.bounds = view.bounds
errorView.backgroundColor = .white
let errorTitleLabel = UILabel()
errorTitleLabel.translatesAutoresizingMaskIntoConstraints = false
errorTitleLabel.text = "Error"
errorTitleLabel.textAlignment = .center
errorView.addSubview(errorTitleLabel)
// .... added constraint to view
// Create a UIGraphicsImageRendererFormat and set its opaque as true
let rendererFormat = UIGraphicsImageRendererFormat.default()
rendererFormat.opaque = true
// Now pass this rendererFormat to UIGraphicsImageRenderer
let renderer = UIGraphicsImageRenderer(size: view.bounds.size, format: rendererFormat)
let image = renderer.image { _ in
errorView.drawHierarchy(in: errorView.bounds, afterScreenUpdates: true)
}
return image
}
Since your crash log suggests on creating PDFPage
it checks for is_image_alpha_opaque
or not. And this provided image is not opaque it will crash.
Here since UIGraphicsImageRenderer
now takes UIGraphicsImageRendererFormat
as input parameter.
We can utilise it's ability to for drawHierarchy where we have set its opaque
property to true and doesn't updated any alpha
.
I hope this will avoid the crash. Happy Coding ✌️
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论