diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index b3a430369..928e61e2c 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -782,6 +782,10 @@ extension AppDelegate { } } + @IBAction func openImageCacheFolder(_ sender: Any?) { + NSWorkspace.shared.open(AppConfig.cacheFolder) + } + @IBAction func showCloudKitStats(_ sender: Any?) { if cloudKitStatsWindowController == nil { cloudKitStatsWindowController = CloudKitStatsWindowController() diff --git a/Mac/Base.lproj/Main.storyboard b/Mac/Base.lproj/Main.storyboard index 2521231a5..6edf58fe2 100644 --- a/Mac/Base.lproj/Main.storyboard +++ b/Mac/Base.lproj/Main.storyboard @@ -555,6 +555,13 @@ + + + + + + + diff --git a/Modules/Account/.swiftpm/xcode/xcshareddata/xcschemes/Account.xcscheme b/Modules/Account/.swiftpm/xcode/xcshareddata/xcschemes/Account.xcscheme index dfa92e25a..b69920923 100644 --- a/Modules/Account/.swiftpm/xcode/xcshareddata/xcschemes/Account.xcscheme +++ b/Modules/Account/.swiftpm/xcode/xcshareddata/xcschemes/Account.xcscheme @@ -1,6 +1,6 @@ CGImage? { + /// Scales image data down to maxPixelSize and returns the result as PNG Data. + /// If the image is a single frame already at or below maxPixelSize, returns + /// the original data unchanged. For multi-image sources (like ICO), extracts + /// the largest frame. Returns nil if the data can't be decoded as an image. + static func scaledImageData(_ data: Data, maxPixelSize: Int) -> Data? { guard let imageSource = CGImageSourceCreateWithData(data as CFData, nil) else { - if debugLoggingEnabled { - RSImageLogger.debug("RSImageLogger: couldn’t create image source") - } - return nil - } - - let numberOfImages = CGImageSourceGetCount(imageSource) - if debugLoggingEnabled { - RSImageLogger.debug("RSImageLogger: numberOfImages == \(numberOfImages, privacy: .public)") - } - guard numberOfImages > 0 else { - return nil - } - - var exactMatch: (index: Int, maxDimension: Int)? - var goodMatch: (index: Int, maxDimension: Int)? - var smallMatch: (index: Int, maxDimension: Int)? - - // Single pass through all images to find the best match - for i in 0.. 0 && height > 0 else { - continue - } - - // Check for exact match (largest dimension equals maxPixelSize) - if maxDimension == maxPixelSize { - exactMatch = (i, maxDimension) - if debugLoggingEnabled { - RSImageLogger.debug("RSImageLogger: found exact match for maxPixelSize: \(maxPixelSize, privacy: .public)") - } - break // Exact match is best, stop searching - } - - // Check for good larger match - if maxDimension > maxPixelSize && maxDimension <= maxPixelSize * 4 { - if let currentGoodMatch = goodMatch { - if maxDimension < currentGoodMatch.maxDimension { - goodMatch = (i, maxDimension) // Prefer smaller size in this range - } - } else { - goodMatch = (i, maxDimension) - } - if debugLoggingEnabled { - RSImageLogger.debug("RSImageLogger: found good match \(maxDimension, privacy: .public) for maxPixelSize: \(maxPixelSize, privacy: .public)") - } - } - - // Check for small match (smaller than maxPixelSize) - if maxDimension < maxPixelSize { - if let currentSmallMatch = smallMatch { - if maxDimension > currentSmallMatch.maxDimension { - smallMatch = (i, maxDimension) // Prefer larger size in this range - } - } else { - smallMatch = (i, maxDimension) - } - if debugLoggingEnabled { - RSImageLogger.debug("RSImageLogger: found small match \(maxDimension, privacy: .public) for maxPixelSize: \(maxPixelSize, privacy: .public)") - } - } - } - - // Return best match in order of preference: exact > good > small - if let match = exactMatch ?? goodMatch ?? smallMatch { - return CGImageSourceCreateImageAtIndex(imageSource, match.index, nil) - } - - // Fallback to creating a thumbnail - if debugLoggingEnabled { - RSImageLogger.debug("RSImageLogger: found no match — calling createThumbnail") - } - return RSImage.createThumbnail(imageSource, maxPixelSize: maxPixelSize) - } - - /// Create a thumbnail from a CGImageSource. - /// - /// - Parameters: - /// - imageSource: The `CGImageSource` from which to create the thumbnail. - /// - maxPixelSize: The maximum dimension of the resulting image. - static func createThumbnail(_ imageSource: CGImageSource, maxPixelSize: Int) -> CGImage? { - guard maxPixelSize > 0 else { return nil } @@ -242,13 +144,72 @@ public extension RSImage { return nil } - if debugLoggingEnabled { - RSImageLogger.debug("RSImageLogger: createThumbnail image source count = \(count, privacy: .public)") + // Find the largest frame in the image source. + var largestIndex = 0 + var largestWidth = 0 + var largestHeight = 0 + var largestMaxDimension = 0 + + for i in 0.. largestMaxDimension { + largestIndex = i + largestWidth = width + largestHeight = height + largestMaxDimension = maxDimension + } } - let options = [kCGImageSourceCreateThumbnailWithTransform: true, - kCGImageSourceCreateThumbnailFromImageIfAbsent: true, - kCGImageSourceThumbnailMaxPixelSize: NSNumber(value: maxPixelSize)] - return CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options as CFDictionary) + guard largestMaxDimension > 0 else { + return nil + } + + let needsResize = largestMaxDimension > maxPixelSize + let isMultiImage = count > 1 + + // Single-frame image already small enough — return original data as-is. + if !needsResize && !isMultiImage { + RSImageLogger.info("Image already small enough: \(largestWidth, privacy: .public)x\(largestHeight, privacy: .public) <= \(maxPixelSize, privacy: .public)px max") + return data + } + + // Either too large or multi-image — extract/resize to a single PNG. + let cgImage: CGImage? + if needsResize { + let options: [CFString: Any] = [ + kCGImageSourceCreateThumbnailWithTransform: true, + kCGImageSourceCreateThumbnailFromImageIfAbsent: true, + kCGImageSourceThumbnailMaxPixelSize: maxPixelSize + ] + cgImage = CGImageSourceCreateThumbnailAtIndex(imageSource, largestIndex, options as CFDictionary) + } else { + cgImage = CGImageSourceCreateImageAtIndex(imageSource, largestIndex, nil) + } + + guard let cgImage else { + return nil + } + + guard let mutableData = CFDataCreateMutable(nil, 0), + let destination = CGImageDestinationCreateWithData(mutableData, UTType.png.identifier as CFString, 1, nil) else { + return nil + } + CGImageDestinationAddImage(destination, cgImage, nil) + guard CGImageDestinationFinalize(destination) else { + return nil + } + + if needsResize { + RSImageLogger.info("Resized image from \(largestWidth, privacy: .public)x\(largestHeight, privacy: .public) to \(cgImage.width, privacy: .public)x\(cgImage.height, privacy: .public)") + } else { + RSImageLogger.info("Extracted largest frame (\(largestWidth, privacy: .public)x\(largestHeight, privacy: .public)) from multi-image source (\(count, privacy: .public) frames)") + } + + return mutableData as Data } } diff --git a/Modules/RSMarkdown/.swiftpm/xcode/xcshareddata/xcschemes/RSMarkdown.xcscheme b/Modules/RSMarkdown/.swiftpm/xcode/xcshareddata/xcschemes/RSMarkdown.xcscheme index 6b4ba1cfb..791750dbb 100644 --- a/Modules/RSMarkdown/.swiftpm/xcode/xcshareddata/xcschemes/RSMarkdown.xcscheme +++ b/Modules/RSMarkdown/.swiftpm/xcode/xcshareddata/xcschemes/RSMarkdown.xcscheme @@ -1,6 +1,6 @@ RSImage? { - let scaledMaxPixelSize = Int(ceil(CGFloat(RSImage.maxIconSize) * RSScreen.maxScreenScale)) - guard var cgImage = RSImage.scaleImage(data, maxPixelSize: scaledMaxPixelSize) else { - return nil - } - - #if os(iOS) - return RSImage(cgImage: cgImage) - #else - let size = NSSize(width: cgImage.width, height: cgImage.height) - return RSImage(cgImage: cgImage, size: size) - #endif - } + static let maxIconPixelSize = Int(ceil(48.0 * RSScreen.maxScreenScale)) static var appIconImage: RSImage? { #if os(macOS) @@ -60,26 +42,3 @@ extension IconImage { static let nnwFeedIcon = IconImage(Assets.Images.nnwFeedIcon) } - -// MARK: - IconScalerQueue - -private final class IconScalerQueue: Sendable { - - static let shared = IconScalerQueue() - - private let queue: DispatchQueue = { - let q = DispatchQueue(label: "IconScaler", attributes: .initiallyInactive) - q.setTarget(queue: DispatchQueue.global(qos: .default)) - q.activate() - return q - }() - - func scaledForIcon(_ data: Data, _ imageResultBlock: @escaping ImageResultBlock) { - queue.async { - let image = RSImage.scaledForIcon(data) - DispatchQueue.main.async { - imageResultBlock(image) - } - } - } -} diff --git a/Shared/Favicons/SingleFaviconDownloader.swift b/Shared/Favicons/SingleFaviconDownloader.swift index 5d2279ee0..ce4e59118 100644 --- a/Shared/Favicons/SingleFaviconDownloader.swift +++ b/Shared/Favicons/SingleFaviconDownloader.swift @@ -135,8 +135,9 @@ private extension SingleFaviconDownloader { do { let (data, response) = try await Downloader.shared.download(url) if let data, !data.isEmpty, let response, response.statusIsOK { - saveToDisk(data) - let image = await RSImage.image(data: data) + let scaledData = RSImage.scaledImageData(data, maxPixelSize: RSImage.maxIconPixelSize) ?? data + saveToDisk(scaledData) + let image = await RSImage.image(data: scaledData) return image } diff --git a/Shared/Images/AuthorAvatarDownloader.swift b/Shared/Images/AuthorAvatarDownloader.swift index 79dfd733e..1d90a410c 100644 --- a/Shared/Images/AuthorAvatarDownloader.swift +++ b/Shared/Images/AuthorAvatarDownloader.swift @@ -66,7 +66,7 @@ extension Notification.Name { @MainActor private extension AuthorAvatarDownloader { func scaleAndCacheImageData(_ imageData: Data, _ avatarURL: String) { - RSImage.scaledForIcon(imageData) { (image) in + RSImage.image(with: imageData) { (image) in MainActor.assumeIsolated { if let image { self.handleImageDidBecomeAvailable(avatarURL, image) diff --git a/Shared/Images/FeedIconDownloader.swift b/Shared/Images/FeedIconDownloader.swift index a95517e70..cd0d68b61 100644 --- a/Shared/Images/FeedIconDownloader.swift +++ b/Shared/Images/FeedIconDownloader.swift @@ -181,7 +181,7 @@ private extension FeedIconDownloader { imageResultBlock(nil) return } - RSImage.scaledForIcon(imageData, imageResultBlock: imageResultBlock) + RSImage.image(with: imageData, imageResultBlock: imageResultBlock) } func postFeedIconDidBecomeAvailableNotification(_ feed: Feed) { diff --git a/Shared/Images/ImageDownloader.swift b/Shared/Images/ImageDownloader.swift index 58c53eb12..ad314e266 100644 --- a/Shared/Images/ImageDownloader.swift +++ b/Shared/Images/ImageDownloader.swift @@ -116,8 +116,9 @@ private extension ImageDownloader { let (data, response) = try await Downloader.shared.download(imageURL) if let data, !data.isEmpty, let response, response.statusIsOK { - saveToDisk(url, data) - return data + let scaledData = RSImage.scaledImageData(data, maxPixelSize: RSImage.maxIconPixelSize) ?? data + saveToDisk(url, scaledData) + return scaledData } if let response = response as? HTTPURLResponse, response.statusCode >= HTTPResponseCode.badRequest && response.statusCode <= HTTPResponseCode.notAcceptable {