// // UIImageWithAlpha.swift // Befund // // Created by Artur Savitskiy on 12.09.22. // Copyright © 2022 MVZ Dr. Stein und Kollegen. All rights reserved. // import Foundation extension UIImage { func image(alpha: CGFloat, targetSize: CGSize) -> UIImage? { let size = size let widthRatio = targetSize.width / size.width let heightRatio = targetSize.height / size.height // Figure out what our orientation is, and use that to form the rectangle var newSize: CGSize if(widthRatio > heightRatio) { newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio) } else { newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio) } let rect = CGRect(origin: .zero, size: newSize) UIGraphicsBeginImageContextWithOptions(newSize, false, scale) draw(in: rect, blendMode: .normal, alpha: alpha) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } }