Advertisement
KY1VSTAR

#4

May 25th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.85 KB | None | 0 0
  1. var thumbnailImage: UIImage {
  2.         let imageSize: CGFloat = 100
  3.         UIGraphicsBeginImageContextWithOptions(CGSize(width: imageSize, height: imageSize), false, 0)
  4.         let context = UIGraphicsGetCurrentContext()!
  5.         if let image = isEmpty ? KSAvatarBundle.defaultContactImage : self.image {
  6.             context.beginPath()
  7.             context.addArc(center: CGPoint(x: imageSize / 2, y: imageSize / 2), radius: imageSize / 2, startAngle: 0, endAngle: 2 * .pi, clockwise: false)
  8.             context.closePath()
  9.             context.clip()
  10.            
  11.             let scaleFactor = imageSize / image.size.width
  12.             context.scaleBy(x: scaleFactor, y: scaleFactor)
  13.            
  14.             image.draw(at: .zero)
  15.         } else {
  16.             context.saveGState()
  17.             context.setFillColor(KSAvatarBundle.contactIconColors[colorIndex!].cgColor)
  18.             context.fillEllipse(in: CGRect(x: 0, y: 0, width: imageSize, height: imageSize))
  19.             context.restoreGState()
  20.            
  21.             let text = character! as NSString
  22.             let font = UIFont.systemFont(ofSize: imageSize * 0.55)
  23.             let textSize = text.size(withAttributes: convertToOptionalNSAttributedStringKeyDictionary([convertFromNSAttributedStringKey(NSAttributedString.Key.font): font]))
  24.             let textRect = CGRect(x: imageSize / 2 - textSize.width / 2, y: imageSize / 2 - textSize.height / 2, width: textSize.width, height: textSize.height)
  25.             text.draw(in: textRect, withAttributes: convertToOptionalNSAttributedStringKeyDictionary([convertFromNSAttributedStringKey(NSAttributedString.Key.font): font, convertFromNSAttributedStringKey(NSAttributedString.Key.foregroundColor): UIColor.white]))
  26.         }
  27.        
  28.         let img = UIGraphicsGetImageFromCurrentImageContext()!
  29.         UIGraphicsEndImageContext()
  30.         return img
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement