32 lines
896 B
Swift
32 lines
896 B
Swift
//
|
|
// https://mczachurski.dev
|
|
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
|
// Licensed under the MIT License.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftUI
|
|
|
|
extension UIImage {
|
|
func roundedAvatar(avatarShape: AvatarShape) -> UIImage {
|
|
let imageView: UIImageView = UIImageView(image: self)
|
|
let layer = imageView.layer
|
|
layer.masksToBounds = true
|
|
|
|
switch avatarShape {
|
|
case .circle:
|
|
layer.cornerRadius = imageView.frame.width / 2
|
|
case .roundedRectangle:
|
|
layer.cornerRadius = imageView.frame.width / 6
|
|
}
|
|
|
|
UIGraphicsBeginImageContext(imageView.bounds.size)
|
|
layer.render(in: UIGraphicsGetCurrentContext()!)
|
|
|
|
let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
|
|
UIGraphicsEndImageContext()
|
|
|
|
return roundedImage!
|
|
}
|
|
}
|