mirror of
https://github.com/danbee/persephone
synced 2025-03-04 08:39:11 +00:00
This brings memory usage (for my music library) down from 2+GB to less
than 300MB. 👍🏼
37 lines
899 B
Swift
37 lines
899 B
Swift
//
|
|
// NSImage.swift
|
|
// Persephone
|
|
//
|
|
// Created by Daniel Barber on 2019/2/19.
|
|
// Copyright © 2019 Dan Barber. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
extension NSImage {
|
|
static let playIcon = NSImage(named: "playButton")
|
|
static let pauseIcon = NSImage(named: "pauseButton")
|
|
|
|
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]
|
|
)
|
|
}
|
|
}
|