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) {
|
|
|
|
DispatchQueue.global().async {
|
|
|
|
let image = RSImage.scaledForAvatar(data)
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
imageResultBlock(image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 07:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension RSImage {
|
|
|
|
|
2019-04-12 00:53:03 +02:00
|
|
|
static func scaledForAvatar(_ data: Data) -> RSImage? {
|
|
|
|
let scaledMaxPixelSize = Int(ceil(CGFloat(RSImage.avatarSize) * RSScreen.mainScreenScale))
|
|
|
|
guard let cgImage = RSImage.scaleImage(data, maxPixelSize: scaledMaxPixelSize) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
#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)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|