Skip to content

Use Image to Generate PDF on iOS 17, there is crash #366

@wangzhizhou

Description

@wangzhizhou

What did you do?

I want use image to generate a pdf file, on iOS 17, there is crash occured!

What did you expect to happen?

generate images pdf file as expected, but crashed

What happened instead?

image

*** Assertion failure in void _UIGraphicsBeginImageContextWithOptions(CGSize, BOOL, CGFloat, BOOL)(), UIGraphics.m:410
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIGraphicsBeginImageContext() failed to allocate CGBitampContext: size={0, 0}, scale=1.000000, bitmapInfo=0x2002. Use UIGraphicsImageRenderer to avoid this assert.'
*** First throw call stack:
(0x1882cab28 0x180142f78 0x18772fa5c 0x18a54e33c 0x102c4cdfc 0x102c4c53c 0x102c5ed74 0x102c0b41c 0x102c079a0 0x102c06848 0x102c06748 0x102c0f740 0x102c0fa00 0x102b15014 0x102b15b1c 0x104634b98 0x1046367bc 0x10463930c 0x10464aae4 0x10464b4d8 0x1e41c0ee4 0x1e41c0fc0)
libc++abi: terminating due to uncaught exception of type NSException

TPPDF Environment

TPPDF version: 2.4.1
Xcode version: 15.3
Swift version: 5.10

Demo Code / Project

@IBAction func pdfBtnAction(_ sender: UIBarButtonItem) {
        sender.isEnabled = false
        DispatchQueue.global().async {
            guard let localComicDir = self.localComicDir,
                  let comicNameSubstring = localComicDir.split(separator: "/").last
            else {
                return
            }
            let name = String(comicNameSubstring)
            let document = PDFDocument(format: .b5)
            document.add(.contentCenter, text: name)
            self.images?[0..<10].compactMap({ imageFileUrl -> PDFImage? in
                guard let imageURL = URL(string: imageFileUrl),
                      let image = UIImage(contentsOfFile: imageURL.path(percentEncoded: false)),
                      image.size != .zero
                else {
                    return nil
                }
                
                let pdfImage = PDFImage(image: image)
                return pdfImage
            }).forEach({ pdfImage in
                document.add(image: pdfImage)
            })
            let generator = PDFGenerator(document: document)
            let info = PDFInfo()
            info.title = name
            do {
                let pdfFileURL = try generator.generateURL(filename: "test.pdf", info: info)
                DispatchQueue.main.async {
                    self.airDropFile(at: pdfFileURL)
                    sender.isEnabled = true
                }
            } catch {
                DispatchQueue.main.async {
                    self.toast(error.localizedDescription)
                    sender.isEnabled = true
                }
            }
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions