fix: 固定首页常用应用图标尺寸
This commit is contained in:
@ -8,14 +8,49 @@ import UIKit
|
||||
/// 首页 / 全部功能菜单图标加载,统一 SF Symbol 与资源图的视觉尺寸。
|
||||
enum HomeMenuIconFactory {
|
||||
|
||||
/// 菜单图标统一使用的逻辑画布尺寸。
|
||||
static let canvasSize = CGSize(width: 24, height: 24)
|
||||
|
||||
/// 与菜单图标槽位(约 24–26pt)对齐的系统图标配置。
|
||||
static let symbolConfiguration = UIImage.SymbolConfiguration(pointSize: 18, weight: .medium)
|
||||
|
||||
/// 优先加载 Assets 资源图;否则按统一配置加载 SF Symbol。
|
||||
private static let imageCache = NSCache<NSString, UIImage>()
|
||||
|
||||
/// 优先加载 Assets 资源图;否则加载 SF Symbol,并统一渲染到固定画布。
|
||||
static func image(named iconName: String) -> UIImage? {
|
||||
if let asset = UIImage(named: iconName) {
|
||||
return asset
|
||||
let cacheKey = iconName as NSString
|
||||
if let cachedImage = imageCache.object(forKey: cacheKey) {
|
||||
return cachedImage
|
||||
}
|
||||
return UIImage(systemName: iconName, withConfiguration: symbolConfiguration)
|
||||
|
||||
guard let sourceImage = UIImage(named: iconName)
|
||||
?? UIImage(systemName: iconName, withConfiguration: symbolConfiguration) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let normalizedImage = renderOnFixedCanvas(sourceImage)
|
||||
imageCache.setObject(normalizedImage, forKey: cacheKey)
|
||||
return normalizedImage
|
||||
}
|
||||
|
||||
private static func renderOnFixedCanvas(_ image: UIImage) -> UIImage {
|
||||
let sourceSize = image.size
|
||||
guard sourceSize.width > 0, sourceSize.height > 0 else {
|
||||
return image
|
||||
}
|
||||
|
||||
let scale = min(canvasSize.width / sourceSize.width, canvasSize.height / sourceSize.height)
|
||||
let drawSize = CGSize(width: sourceSize.width * scale, height: sourceSize.height * scale)
|
||||
let drawRect = CGRect(
|
||||
x: (canvasSize.width - drawSize.width) / 2,
|
||||
y: (canvasSize.height - drawSize.height) / 2,
|
||||
width: drawSize.width,
|
||||
height: drawSize.height
|
||||
)
|
||||
let renderer = UIGraphicsImageRenderer(size: canvasSize)
|
||||
let renderedImage = renderer.image { _ in
|
||||
image.withRenderingMode(.alwaysOriginal).draw(in: drawRect)
|
||||
}
|
||||
return renderedImage.withRenderingMode(.alwaysTemplate)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user