Skip to content

Commit 9614368

Browse files
committed
Improve quality of app icon sent to Pulse Pro
1 parent e1529aa commit 9614368

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

Sources/Pulse/Helpers/ImageProcessor.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ import ImageIO
1919
#endif
2020

2121
enum Graphics {
22+
static func resize(_ image: UIImage, to size: CGSize) -> PlatformImage? {
23+
#if os(macOS)
24+
let newImage = NSImage(size: newSize)
25+
newImage.lockFocus()
26+
let sourceRect = NSMakeRect(0, 0, size.width, size.height)
27+
let destRect = NSMakeRect(0, 0, newSize.width, newSize.height)
28+
draw(in: destRect, from: sourceRect, operation: .sourceOver, fraction: CGFloat(1))
29+
newImage.unlockFocus()
30+
return newImage
31+
#else
32+
UIGraphicsBeginImageContextWithOptions(size, false, 1.0)
33+
image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
34+
let thumbnail = UIGraphicsGetImageFromCurrentImageContext()
35+
UIGraphicsEndImageContext()
36+
return thumbnail
37+
#endif
38+
}
39+
2240
/// Creates an image thumbnail. Uses significantly less memory than other options.
2341
static func makeThumbnail(from data: Data, targetSize: CGFloat) -> PlatformImage? {
2442
guard let source = CGImageSourceCreateWithData(data as CFData, [kCGImageSourceShouldCache: false] as CFDictionary) else {

Sources/Pulse/LoggerStore/LoggerStore+Info.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ private func getAppIcon() -> Data? {
9696
let files = primaryIcons["CFBundleIconFiles"] as? [String],
9797
let lastIcon = files.last,
9898
let image = PlatformImage(named: lastIcon),
99-
let data = Graphics.encode(image),
100-
let thumbnail = Graphics.makeThumbnail(from: data, targetSize: 32) else { return nil }
101-
return Graphics.encode(thumbnail)
99+
let thumbnail = Graphics.resize(image, to: CGSize(width: 44, height: 44)) else { return nil }
100+
return Graphics.encode(thumbnail, compressionQuality: 0.9)
102101
}
103102

104103
#if os(iOS) || os(tvOS) || os(visionOS)

Sources/PulseUI/Helpers/FileViewModelContext.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
// Copyright (c) 2020-2024 Alexander Grebenyuk (github.com/kean).
44

55
import Pulse
6+
import Foundation
67

78
struct FileViewerViewModelContext {
89
var contentType: NetworkLogger.ContentType?
910
var originalSize: Int64
1011
var metadata: [String: String]?
1112
var isResponse = true
1213
var error: NetworkLogger.DecodingError?
14+
var sourceURL: URL?
1315
}
1416

1517
extension NetworkTaskEntity {
@@ -29,7 +31,8 @@ extension NetworkTaskEntity {
2931
originalSize: responseBodySize,
3032
metadata: metadata,
3133
isResponse: true,
32-
error: decodingError
34+
error: decodingError,
35+
sourceURL: currentRequest?.url.flatMap(URL.init)
3336
)
3437
}
3538

Sources/PulseUI/Views/ImageViewer.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct ImageThumbnailView: View {
3737
struct ImagePreviewViewModel {
3838
let image: UXImage
3939
let info: KeyValueSectionViewModel
40+
let context: FileViewerViewModelContext
4041

4142
init(image: UXImage, data: Data, context: FileViewerViewModelContext) {
4243
func intValue(for key: String) -> Int? {
@@ -67,11 +68,12 @@ struct ImagePreviewViewModel {
6768
("Resolution", originalImageSize.map(formattedResolution)),
6869
("Size", ByteCountFormatter.string(fromByteCount: context.originalSize)),
6970
("Type", context.contentType?.rawValue),
70-
("Source", isShowingOriginal ? "Original" : "Thumbnail (\(formattedResolution(with: image.size)))")
71+
("Stored", isShowingOriginal ? "Original" : "Thumbnail (\(formattedResolution(with: image.size)))")
7172
]
7273

7374
self.image = image
7475
self.info = KeyValueSectionViewModel(title: "Image", color: .pink, items: info)
76+
self.context = context
7577
}
7678
}
7779

0 commit comments

Comments
 (0)