mirror of
https://github.com/danbee/persephone
synced 2025-03-04 08:39:11 +00:00
Scale down cover images
This brings memory usage (for my music library) down from 2+GB to less
than 300MB. 👍🏼
This commit is contained in:
parent
280ec0cdc4
commit
e8b58b7686
@ -13,4 +13,24 @@ extension NSImage {
|
|||||||
static let pauseIcon = NSImage(named: "pauseButton")
|
static let pauseIcon = NSImage(named: "pauseButton")
|
||||||
|
|
||||||
static let defaultCoverArt = NSImage(named: "blankAlbum")
|
static let defaultCoverArt = NSImage(named: "blankAlbum")
|
||||||
|
|
||||||
|
func toFitBox(size: NSSize) -> NSImage {
|
||||||
|
let newImage = NSImage(size: size)
|
||||||
|
newImage.lockFocus()
|
||||||
|
self.draw(in: newImage.alignmentRect)
|
||||||
|
newImage.unlockFocus()
|
||||||
|
return newImage
|
||||||
|
}
|
||||||
|
|
||||||
|
func jpegData(compressionQuality: CGFloat) -> Data? {
|
||||||
|
guard let image = cgImage(forProposedRect: nil, context: nil, hints: nil)
|
||||||
|
else { return nil }
|
||||||
|
|
||||||
|
let bitmapImageRep = NSBitmapImageRep(cgImage: image)
|
||||||
|
|
||||||
|
return bitmapImageRep.representation(
|
||||||
|
using: .jpeg,
|
||||||
|
properties: [.compressionFactor: compressionQuality]
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -76,8 +76,15 @@ class AlbumArtService: NSObject {
|
|||||||
if FileManager.default.fileExists(atPath: coverArtURI),
|
if FileManager.default.fileExists(atPath: coverArtURI),
|
||||||
let data = FileManager.default.contents(atPath: coverArtURI),
|
let data = FileManager.default.contents(atPath: coverArtURI),
|
||||||
let image = NSImage(data: data) {
|
let image = NSImage(data: data) {
|
||||||
self.cacheArtwork(for: album, data: data)
|
|
||||||
callback(image)
|
let imageThumb = image.toFitBox(
|
||||||
|
size: NSSize(width: 180, height: 180)
|
||||||
|
)
|
||||||
|
self.cacheArtwork(
|
||||||
|
for: album,
|
||||||
|
data: imageThumb.jpegData(compressionQuality: 0.5)
|
||||||
|
)
|
||||||
|
callback(imageThumb)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,7 +92,7 @@ class AlbumArtService: NSObject {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func cacheArtwork(for album: AlbumItem, data: Data) {
|
func cacheArtwork(for album: AlbumItem, data: Data?) {
|
||||||
guard let bundleIdentifier = Bundle.main.bundleIdentifier,
|
guard let bundleIdentifier = Bundle.main.bundleIdentifier,
|
||||||
let cacheDir = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
let cacheDir = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
||||||
.appendingPathComponent(bundleIdentifier)
|
.appendingPathComponent(bundleIdentifier)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user