Skip to content

Commit 04f8f89

Browse files
committed
Add cost parameter and use MediaSource internally
- Add cost parameter to NSCache setObject calls for memory management - Convert URL to MediaSource once in MatrixClient.loadImage() - Update .gitignore to exclude build/ directory
1 parent 5ec3d67 commit 04f8f89

4 files changed

Lines changed: 8 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
xcuserdata/
22
.DS_Store
3+
build/

Mactrix/Models/MatrixClient.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,19 +255,21 @@ extension MatrixClient: UI.ImageLoader {
255255
return Image(nsImage: cached)
256256
}
257257

258+
let mediaSource = try MediaSource.fromUrl(url: matrixUrl)
259+
258260
let imageData: Data
259261
if let size {
260262
let width = UInt64(size.width)
261263
let height = UInt64(size.height)
262-
imageData = try await client.getMediaThumbnail(mediaSource: .fromUrl(url: matrixUrl), width: UInt64(width), height: UInt64(height))
264+
imageData = try await client.getMediaThumbnail(mediaSource: mediaSource, width: width, height: height)
263265
} else {
264-
imageData = try await client.getMediaContent(mediaSource: .fromUrl(url: matrixUrl))
266+
imageData = try await client.getMediaContent(mediaSource: mediaSource)
265267
}
266268

267269
do {
268270
let image = try imageData.toOrientedImage(contentType: imageData.computeMimeType())
269271
if let nsImage = NSImage(data: imageData) {
270-
Self.imageCache.setObject(nsImage, forKey: cacheKey)
272+
Self.imageCache.setObject(nsImage, forKey: cacheKey, cost: imageData.count)
271273
}
272274
return image
273275
} catch {

Mactrix/Views/ChatView/MessageImageView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ struct MessageImageView: View {
107107
imageData = data
108108
image = try data.toOrientedImage(contentType: contentType)
109109
if let nsImage = NSImage(data: data) {
110-
MatrixClient.imageCache.setObject(nsImage, forKey: cacheKey)
110+
MatrixClient.imageCache.setObject(nsImage, forKey: cacheKey, cost: data.count)
111111
}
112112
} catch {
113113
errorMessage = error.localizedDescription

Mactrix/Views/MatrixImageView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct MatrixImageView: View {
6060
let contentType = mimeType.flatMap { UTType(mimeType: $0) }
6161
image = try await Image(importing: data, contentType: contentType)
6262
if let nsImage = NSImage(data: data) {
63-
MatrixClient.imageCache.setObject(nsImage, forKey: cacheKey)
63+
MatrixClient.imageCache.setObject(nsImage, forKey: cacheKey, cost: data.count)
6464
}
6565
} catch {
6666
errorMessage = error.localizedDescription

0 commit comments

Comments
 (0)