Starting iOS 16 seeing some crashes related to pdf printing in the crash reporter. It looks like the issue is not so frequent. Also, I'm unable to reproduce the crash. Looks like the app crashes when the print preview dialog is opening. According to crash reports, there are some crashes on different iOS 16 versions: 16.0.0, 16.0.2, and 16.0.3.
Printing code:
let printInfo = UIPrintInfo.printInfo()
printInfo.jobName = "Printing Job Name"
self.printViewController = UIPrintInteractionController.shared
self.printViewController?.printInfo = printInfo
self.printViewController?.printingItem = pdfURL
self.printViewController?.present(from: barButtonItem, animated: true) { (controller, completed, error) in
self.printViewController = nil
}
Stack trace:
compare_key + 4 (CGPDFObject.c:134)
bsearch + 68 (bsearch.c:70)
CGPDFDictionaryGetUnresolvedObject + 68 (CGPDFDictionary.c:153)
CGPDFDictionaryGetObject + 44 (CGPDFDictionary.c:172)
CGPDFDictionaryGetDictionary + 44 (CGPDFDictionary.c:284)
get_pages_dictionary + 68 (pdf-reader.c:410)
pdf_reader_get_number_of_pages + 76 (pdf-reader.c:557)
-[UIPrintPreviewPageFetcher fetchNumberOfItems] + 76 (UIPrintPreviewPageFetcher.m:115)
-[UIPrintPreviewViewController collectionView:numberOfItemsInSection:] + 32 (UIPrintPreviewViewController.m:482)
-[UICollectionViewData _updateItemCounts] + 220 (UICollectionViewData.mm:335)
-[UICollectionViewData isIndexPathValid:validateItemCounts:] + 52 (UICollectionViewData.mm:348)
-[UICollectionViewData validatedGlobalIndexForItemAtIndexPath:] + 36 (UICollectionViewData.mm:778)
-[UICollectionView _cellForItemAtIndexPath:] + 108 (UICollectionView.m:7112)
-[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:collectionViewAnimator:] + 1384 (UICollectionView.m:9357)
-[UICollectionView _updateRowsAtIndexPaths:updateAction:updates:] + 396 (UICollectionView.m:9104)
-[UICollectionView reloadItemsAtIndexPaths:] + 52 (UICollectionView.m:9124)
-[UIPrintPreviewViewController reloadVisibleItems:] + 256 (UIPrintPreviewViewController.m:568)
__63-[UIPrintPreviewViewController updatePdfURL:options:completed:]_block_invoke_2 + 44 (UIPrintPreviewViewController.m:305)
__NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 24 (NSOperation.m:1545)
-[NSBlockOperation main] + 104 (NSOperation.m:1564)
__NSOPERATION_IS_INVOKING_MAIN__ + 16 (NSOperation.m:2189)
-[NSOperation start] + 708 (NSOperation.m:2206)
Thanks!
I found out the cause and solved it. The problem is that when the print window is displayed, the result rendered by the UIPrintPageRenderer is previewed in real time. At this point, something crashes as rendering and previewing happen simultaneously. This is considered an API bug. The way to avoid it is not to render in real time in the print window, but to open the print window after setting the pre-rendered PDF file to printingItem before opening the print window. I didn't get any app crashes when I did this.
UIPrintInteractionController *controller = (UIPrintInteractionController*)m_pController;
CGSize pageSize = CGSizeMake(595.2, 841.8); // default A4
UIEdgeInsets pageMargins = UIEdgeInsetsMake(72, 72, 72,72);
CGRect printableRect = CGRectMake(pageMargins.left, pageMargins.top,
pageSize.width - pageMargins.left - pageMargins.right,
pageSize.height - pageMargins.top - pageMargins.bottom);
CGRect paperRect = CGRectMake(0, 0, pageSize.width, pageSize.height);
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData( pdfData, paperRect, nil );
[self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)];
CGRect bounds = UIGraphicsGetPDFContextBounds();
for ( int i = 0 ; i < [self numberOfPages] ; i++ ) {
UIGraphicsBeginPDFPage();
[self drawPageAtIndex: i inRect: bounds];
}
UIGraphicsEndPDFContext();
controller.printingItem = pdfdata;