美文网首页
extension UIImage:为图片设置头像圆圈

extension UIImage:为图片设置头像圆圈

作者: _浅墨_ | 来源:发表于2022-09-04 13:50 被阅读0次
    import UIKit
    
    
    extension UIImage {
        
        var isPortrait: Bool { return size.height > size.width }
        var isLandscape: Bool { return size.width > size.height }
        var breadth: CGFloat { return min(size.width, size.height) }
        var breadthSize: CGSize { return CGSize(width: breadth, height: breadth) }
        var breadthRect: CGRect { return CGRect(origin: .zero, size: breadthSize) }
        
        var circleMasked: UIImage? {
            
            UIGraphicsBeginImageContextWithOptions(breadthSize, false, scale)
            
            defer { UIGraphicsEndImageContext() }
            guard let cgImage = cgImage?.cropping(to: CGRect(origin: CGPoint(x: isLandscape ? floor((size.width - size.height) / 2) : 0, y: isPortrait ? floor((size.height - size.width) / 2) : 0), size: breadthSize)) else { return nil }
            
            UIBezierPath(ovalIn: breadthRect).addClip()
            UIImage(cgImage: cgImage).draw(in: breadthRect)
            return UIGraphicsGetImageFromCurrentImageContext()
        }
        
    }
    
    
    extension Date {
        
        func longDate() -> String {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "dd MMM yyyy"
            return dateFormatter.string(from: self)
        }
        
        func stringDate() -> String {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "ddMMMyyyyHHmmss"
            return dateFormatter.string(from: self)
        }
        
        func time() -> String {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "HH:mm"
            return dateFormatter.string(from: self)
        }
        
        func interval(ofComponent comp: Calendar.Component, from date: Date) -> Float {
            
            let currentCalendar = Calendar.current
            
            guard  let start = currentCalendar.ordinality(of: comp, in: .era, for: date) else { return 0 }
            guard  let end = currentCalendar.ordinality(of: comp, in: .era, for: self) else { return 0 }
    
            return Float(start - end)
        }
    
    }
    

    相关文章

      网友评论

          本文标题:extension UIImage:为图片设置头像圆圈

          本文链接:https://www.haomeiwen.com/subject/cthmnrtx.html