From 62ace2583d5f0416bbb48f661dc728bb7c15cac9 Mon Sep 17 00:00:00 2001 From: Miltiadis Vasilakis Date: Fri, 15 Nov 2024 17:45:32 +0200 Subject: [PATCH] Auto release computed PNG data after caching --- .../AnnotationPreviewController.swift | 20 ++++++++++--------- .../Controllers/PDFThumbnailController.swift | 18 +++++++++-------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Zotero/Controllers/AnnotationPreviewController.swift b/Zotero/Controllers/AnnotationPreviewController.swift index 0391f253d..4a22d310e 100644 --- a/Zotero/Controllers/AnnotationPreviewController.swift +++ b/Zotero/Controllers/AnnotationPreviewController.swift @@ -306,15 +306,17 @@ extension AnnotationPreviewController { } private func cache(image: UIImage, key: String, pdfKey: String, libraryId: LibraryIdentifier, isDark: Bool) { - guard let data = image.pngData() else { - DDLogError("AnnotationPreviewController: can't create data from image") - return - } - - do { - try fileStorage.write(data, to: Files.annotationPreview(annotationKey: key, pdfKey: pdfKey, libraryId: libraryId, isDark: isDark), options: .atomicWrite) - } catch let error { - DDLogError("AnnotationPreviewController: can't store preview - \(error)") + autoreleasepool { + guard let data = image.pngData() else { + DDLogError("AnnotationPreviewController: can't create data from image") + return + } + + do { + try fileStorage.write(data, to: Files.annotationPreview(annotationKey: key, pdfKey: pdfKey, libraryId: libraryId, isDark: isDark), options: .atomicWrite) + } catch let error { + DDLogError("AnnotationPreviewController: can't store preview - \(error)") + } } } } diff --git a/Zotero/Controllers/PDFThumbnailController.swift b/Zotero/Controllers/PDFThumbnailController.swift index 6831f1998..1dba69ceb 100644 --- a/Zotero/Controllers/PDFThumbnailController.swift +++ b/Zotero/Controllers/PDFThumbnailController.swift @@ -149,14 +149,16 @@ extension PDFThumbnailController { } func cache(image: UIImage, page: UInt, key: String, libraryId: LibraryIdentifier, isDark: Bool) { - guard let data = image.pngData() else { - DDLogError("PdfThumbnailController: can't create data from image") - return - } - do { - try fileStorage.write(data, to: Files.pageThumbnail(pageIndex: page, key: key, libraryId: libraryId, isDark: isDark), options: .atomicWrite) - } catch let error { - DDLogError("PdfThumbnailController: can't store preview - \(error)") + autoreleasepool { + guard let data = image.pngData() else { + DDLogError("PdfThumbnailController: can't create data from image") + return + } + do { + try fileStorage.write(data, to: Files.pageThumbnail(pageIndex: page, key: key, libraryId: libraryId, isDark: isDark), options: .atomicWrite) + } catch let error { + DDLogError("PdfThumbnailController: can't store preview - \(error)") + } } } }