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-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
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
static func scaledForIcon(_ data: Data, imageResultBlock: @escaping (RSImage?) -> Void) {
|
2019-10-31 20:39:35 +01:00
|
|
|
DispatchQueue.global(qos: .default).async {
|
2019-11-06 01:05:57 +01:00
|
|
|
let image = RSImage.scaledForIcon(data)
|
2019-04-12 00:53:03 +02:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
imageResultBlock(image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 07:44:06 +02:00
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
static func scaledForIcon(_ data: Data) -> RSImage? {
|
|
|
|
let scaledMaxPixelSize = Int(ceil(CGFloat(RSImage.maxIconSize) * RSScreen.mainScreenScale))
|
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
|
|
|
}
|
|
|
|
}
|