mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.3 KiB
Swift
42 lines
1.3 KiB
Swift
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import UIKit
|
|
import SessionUtilitiesKit
|
|
|
|
@objc(LKIdenticon)
|
|
public final class Identicon: NSObject {
|
|
|
|
@objc public static func generatePlaceholderIcon(seed: String, text: String, size: CGFloat) -> UIImage {
|
|
let icon = PlaceholderIcon(seed: seed)
|
|
|
|
var content: String = (text.hasSuffix("\(String(seed.suffix(4))))") ?
|
|
(text.split(separator: "(")
|
|
.first
|
|
.map { String($0) })
|
|
.defaulting(to: text) :
|
|
text
|
|
)
|
|
|
|
if content.count > 2 && SessionId.Prefix(from: content) != nil {
|
|
content.removeFirst(2)
|
|
}
|
|
|
|
let initials: String = content
|
|
.split(separator: " ")
|
|
.compactMap { word in word.first.map { String($0) } }
|
|
.joined()
|
|
let layer = icon.generateLayer(
|
|
with: size,
|
|
text: (initials.count >= 2 ?
|
|
initials.substring(to: 2).uppercased() :
|
|
content.substring(to: 2).uppercased()
|
|
)
|
|
)
|
|
|
|
let rect = CGRect(origin: CGPoint.zero, size: layer.frame.size)
|
|
let renderer = UIGraphicsImageRenderer(size: rect.size)
|
|
|
|
return renderer.image { layer.render(in: $0.cgContext) }
|
|
}
|
|
}
|