2019-04-12 00:53:03 +02:00
|
|
|
//
|
|
|
|
// RSImage-Extensions.swift
|
|
|
|
// RSCore
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 4/11/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2020-03-20 06:22:58 +01:00
|
|
|
import RSCore
|
2020-01-17 03:09:18 +01:00
|
|
|
#if os(macOS)
|
|
|
|
import AppKit
|
|
|
|
#else
|
|
|
|
import UIKit
|
|
|
|
#endif
|
|
|
|
|
2019-04-12 00:53:03 +02:00
|
|
|
import RSCore
|
|
|
|
|
|
|
|
extension RSImage {
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
static let maxIconSize = 48
|
2019-04-12 00:53:03 +02:00
|
|
|
|
2020-03-20 06:22:58 +01:00
|
|
|
static func scaledForIcon(_ data: Data, imageResultBlock: @escaping ImageResultBlock) {
|
|
|
|
IconScalerQueue.shared.scaledForIcon(data, imageResultBlock)
|
2019-04-12 00:53:03 +02:00
|
|
|
}
|
2019-05-15 07:44:06 +02:00
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
static func scaledForIcon(_ data: Data) -> RSImage? {
|
2020-03-18 22:08:17 +01:00
|
|
|
let scaledMaxPixelSize = Int(ceil(CGFloat(RSImage.maxIconSize) * RSScreen.maxScreenScale))
|
2019-06-14 22:33:13 +02:00
|
|
|
guard var cgImage = RSImage.scaleImage(data, maxPixelSize: scaledMaxPixelSize) else {
|
2019-04-12 00:53:03 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-09-17 05:07:07 +02:00
|
|
|
|
2019-04-12 00:53:03 +02:00
|
|
|
#if os(iOS)
|
|
|
|
return RSImage(cgImage: cgImage)
|
|
|
|
#else
|
2019-04-13 22:05:45 +02:00
|
|
|
let size = NSSize(width: cgImage.width, height: cgImage.height)
|
2019-04-12 00:53:03 +02:00
|
|
|
return RSImage(cgImage: cgImage, size: size)
|
2019-09-17 05:07:07 +02:00
|
|
|
#endif
|
2019-06-14 22:33:13 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 06:22:58 +01:00
|
|
|
|
|
|
|
// MARK: - IconScalerQueue
|
|
|
|
|
|
|
|
private class IconScalerQueue {
|
|
|
|
|
|
|
|
static let shared = IconScalerQueue()
|
|
|
|
|
|
|
|
private let queue: DispatchQueue = {
|
|
|
|
let q = DispatchQueue(label: "IconScaler", attributes: .initiallyInactive)
|
|
|
|
q.setTarget(queue: DispatchQueue.global(qos: .default))
|
|
|
|
q.activate()
|
|
|
|
return q
|
|
|
|
}()
|
|
|
|
|
|
|
|
func scaledForIcon(_ data: Data, _ imageResultBlock: @escaping ImageResultBlock) {
|
|
|
|
queue.async {
|
|
|
|
let image = RSImage.scaledForIcon(data)
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
imageResultBlock(image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|