Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var thumbnailImage: UIImage {
- let imageSize: CGFloat = 100
- UIGraphicsBeginImageContextWithOptions(CGSize(width: imageSize, height: imageSize), false, 0)
- let context = UIGraphicsGetCurrentContext()!
- if let image = isEmpty ? KSAvatarBundle.defaultContactImage : self.image {
- context.beginPath()
- context.addArc(center: CGPoint(x: imageSize / 2, y: imageSize / 2), radius: imageSize / 2, startAngle: 0, endAngle: 2 * .pi, clockwise: false)
- context.closePath()
- context.clip()
- let scaleFactor = imageSize / image.size.width
- context.scaleBy(x: scaleFactor, y: scaleFactor)
- image.draw(at: .zero)
- } else {
- context.saveGState()
- context.setFillColor(KSAvatarBundle.contactIconColors[colorIndex!].cgColor)
- context.fillEllipse(in: CGRect(x: 0, y: 0, width: imageSize, height: imageSize))
- context.restoreGState()
- let text = character! as NSString
- let font = UIFont.systemFont(ofSize: imageSize * 0.55)
- let textSize = text.size(withAttributes: convertToOptionalNSAttributedStringKeyDictionary([convertFromNSAttributedStringKey(NSAttributedString.Key.font): font]))
- let textRect = CGRect(x: imageSize / 2 - textSize.width / 2, y: imageSize / 2 - textSize.height / 2, width: textSize.width, height: textSize.height)
- text.draw(in: textRect, withAttributes: convertToOptionalNSAttributedStringKeyDictionary([convertFromNSAttributedStringKey(NSAttributedString.Key.font): font, convertFromNSAttributedStringKey(NSAttributedString.Key.foregroundColor): UIColor.white]))
- }
- let img = UIGraphicsGetImageFromCurrentImageContext()!
- UIGraphicsEndImageContext()
- return img
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement