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