From b784abbec13e7bd4a8c951206a04f7df72f062f0 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 4 Oct 2025 08:27:30 -0700 Subject: [PATCH] =?UTF-8?q?Create=20and=20use=20pngData()=20method=20for?= =?UTF-8?q?=20Mac=20to=20get=20data=20from=20image=20as=20PNG=20instead=20?= =?UTF-8?q?of=20TIFF=20=E2=80=94=C2=A0use=20less=20memory.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/RSCore/Shared/RSImage.swift | 25 +++++++++++++------ Shared/Extensions/ArticleUtilities.swift | 4 --- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Modules/RSCore/Sources/RSCore/Shared/RSImage.swift b/Modules/RSCore/Sources/RSCore/Shared/RSImage.swift index 7e8e5caf8..aa7e4220b 100644 --- a/Modules/RSCore/Sources/RSCore/Shared/RSImage.swift +++ b/Modules/RSCore/Sources/RSCore/Shared/RSImage.swift @@ -83,16 +83,25 @@ public extension RSImage { } #endif +#if os(macOS) + /// Returns data as PNG. May be nil in some circumstances. + func pngData() -> Data? { + guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else { + return nil + } + let bitmapRep = NSBitmapImageRep(cgImage: cgImage) + return bitmapRep.representation(using: .png, properties: [:]) + } +#endif + /// Returns a data representation of the image. - /// - /// The resultant data is TIFF data on macOS, and PNG data on iOS. - /// - Returns: Data representing the image. + /// - Returns: Image data. Normally PNG, though in rare cases it might TIFF on macOS. func dataRepresentation() -> Data? { - #if os(macOS) - return tiffRepresentation - #else - return pngData() - #endif +#if os(macOS) + pngData() ?? tiffRepresentation +#else + return pngData() +#endif } /// Asynchronously initializes an image from data. diff --git a/Shared/Extensions/ArticleUtilities.swift b/Shared/Extensions/ArticleUtilities.swift index 1a526f5e0..2deaa3f52 100644 --- a/Shared/Extensions/ArticleUtilities.swift +++ b/Shared/Extensions/ArticleUtilities.swift @@ -126,11 +126,7 @@ extension Article { let fm = FileManager.default var path = fm.urls(for: .cachesDirectory, in: .userDomainMask)[0] let feedID = webFeed.webFeedID.replacingOccurrences(of: "/", with: "_") - #if os(macOS) - path.appendPathComponent(feedID + "_smallIcon.tiff") - #else path.appendPathComponent(feedID + "_smallIcon.png") - #endif fm.createFile(atPath: path.path, contents: image.image.dataRepresentation()!, attributes: nil) return path } else {