diff --git a/Modules/RSCore/Sources/RSCore/Shared/RSImage.swift b/Modules/RSCore/Sources/RSCore/Shared/RSImage.swift index e7d430bb0..7e8e5caf8 100644 --- a/Modules/RSCore/Sources/RSCore/Shared/RSImage.swift +++ b/Modules/RSCore/Sources/RSCore/Shared/RSImage.swift @@ -7,6 +7,7 @@ // import Foundation +import os.log #if os(macOS) import AppKit @@ -18,6 +19,9 @@ import UIKit public typealias RSImage = UIImage #endif +private let RSImageLogger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "RSImage") +private let debugLoggingEnabled = false + public extension RSImage { /// Create a colored image from the source image using a specified color. @@ -112,94 +116,94 @@ public extension RSImage { /// - data: The data object containing the image data. /// - maxPixelSize: The maximum dimension of the image. static func scaleImage(_ data: Data, maxPixelSize: Int) -> CGImage? { - 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 the image size matches exactly, then return it. - for i in 0.. maxPixelSize, but <= maxPixelSize * 2, then return it. - for i in 0.. maxPixelSize * 2 || imagePixelWidth.intValue < maxPixelSize { - continue - } - - guard let imagePixelHeight = imageProperties[kCGImagePropertyPixelHeight] as? NSNumber else { - continue - } - if imagePixelHeight.intValue > maxPixelSize * 2 || imagePixelHeight.intValue < maxPixelSize { - continue - } - - return CGImageSourceCreateImageAtIndex(imageSource, i, nil) + guard numberOfImages > 0 else { + return nil } - - - // If the image data contains a smaller image than the max size, just return it. + + var exactMatch: (index: Int, maxDimension: Int)? = nil + var goodMatch: (index: Int, maxDimension: Int)? = nil + var smallMatch: (index: Int, maxDimension: Int)? = nil + + // Single pass through all images to find the best match for i in 0.. 0 && height > 0 else { continue } - if imagePixelWidth.intValue < 1 || imagePixelWidth.intValue > maxPixelSize { - 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 } - guard let imagePixelHeight = imageProperties[kCGImagePropertyPixelHeight] as? NSNumber else { - continue + // 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)") + } } - if imagePixelHeight.intValue > 0 && imagePixelHeight.intValue <= maxPixelSize { - if let image = CGImageSourceCreateImageAtIndex(imageSource, i, nil) { - return image + + // 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 RSImage.createThumbnail(imageSource, maxPixelSize: maxPixelSize) + // 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. @@ -208,11 +212,19 @@ public extension RSImage { /// - 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 } - + guard maxPixelSize > 0 else { + return nil + } + let count = CGImageSourceGetCount(imageSource) - guard count > 0 else { return nil } - + guard count > 0 else { + return nil + } + + if debugLoggingEnabled { + RSImageLogger.debug("RSImageLogger: createThumbnail image source count = \(count, privacy: .public)") + } + let options = [kCGImageSourceCreateThumbnailWithTransform : true, kCGImageSourceCreateThumbnailFromImageIfAbsent : true, kCGImageSourceThumbnailMaxPixelSize : NSNumber(value: maxPixelSize)]