Advertisement
neatekFb

UIImage solid color // swift 3

Dec 2nd, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.55 KB | None | 0 0
  1. // Vladimir Zhelnov - neatek.pw - Web/iOS dev
  2. let redImage = UIImage(color: .redColor())
  3.  
  4. public extension UIImage {
  5.   public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
  6.     let rect = CGRect(origin: .zero, size: size)
  7.     UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
  8.     color.setFill()
  9.     UIRectFill(rect)
  10.     let image = UIGraphicsGetImageFromCurrentImageContext()
  11.     UIGraphicsEndImageContext()
  12.  
  13.     guard let cgImage = image?.cgImage else { return nil }
  14.     self.init(cgImage: cgImage)
  15.   }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement