// // exts.swift // XuXuCheck // // Created by henseycai on 2025/6/20. // import Foundation import SwiftUI // MARK: - Simple //let bgColor = Color(hex: "E8F2F0") //let alertColor = Color(hex: "ee6c4d") //let topToastColor = Color(hex: "4067CC") //let bottomToastColor = Color(hex: "bfdcae") //let topFloatColor = Color(hex: "61A117") //let bottomFloatColor = Color(hex: "3d5a80") //let leftRightToastColor = Color(hex: "85adad") extension Color { init(hex: String) { let scanner = Scanner(string: hex) var rgbValue: UInt64 = 0 scanner.scanHexInt64(&rgbValue) let r = (rgbValue & 0xff0000) >> 16 let g = (rgbValue & 0xff00) >> 8 let b = rgbValue & 0xff self.init( red: Double(r) / 0xff, green: Double(g) / 0xff, blue: Double(b) / 0xff ) } } struct MyLinearAnimation: CustomAnimation { let duration: TimeInterval func animate( value: V, time: TimeInterval, context: inout AnimationContext ) -> V? where V: VectorArithmetic { guard time < duration else { return nil } return value.scaled(by: time / duration) } } // For convenience and code readability, we extend Animation extension Animation { static func myLinear(duration: TimeInterval) -> Animation { Animation(MyLinearAnimation(duration: duration)) } static var myLinear: Animation { Animation(MyLinearAnimation(duration: 5.0)) } } //https://www.mysamplecode.com/2019/04/ios-swift-convert-image-base64.html func convertImageToBase64(image: UIImage) -> String { let imageData = image.pngData()! return imageData.base64EncodedString( options: Data.Base64EncodingOptions.lineLength64Characters ) } func convertBase64ToImage(imageString: String) -> UIImage { let imageData = Data( base64Encoded: imageString, options: Data.Base64DecodingOptions.ignoreUnknownCharacters )! return UIImage(data: imageData)! } extension UIImage { func getJpegBase64() -> String? { return self.jpegData(compressionQuality: 1)?.base64EncodedString() } func compressedJpegData(quality: CGFloat) -> Data?{ return self.jpegData(compressionQuality: quality) } func aspectFit2Height(_ newHeight: CGFloat) -> UIImage{ let scale = newHeight / self.size.height let newWidth = self.size.width * scale let newSize = CGSize(width: newWidth, height: newHeight) let renderer = UIGraphicsImageRenderer(size: newSize) return renderer.image{ _ in self.draw(in: CGRect(origin: .zero, size: newSize)) } } }