Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. //
  2. // SGImageExtensions.swift
  3. //
  4. // Created by Sudhir Gadhvi on 10/01/19.
  5. // Copyright © 2019 Sudhir. All rights reserved.
  6. //
  7.  
  8. extension UIImage {
  9.  
  10. convenience init?(imageName: String) {
  11. self.init(named: imageName)!
  12. accessibilityIdentifier = imageName
  13. }
  14.  
  15. func imageWithColor (newColor: UIColor?) -> UIImage? {
  16.  
  17. if let newColor = newColor {
  18. UIGraphicsBeginImageContextWithOptions(size, false, scale)
  19.  
  20. let context = UIGraphicsGetCurrentContext()!
  21. context.translateBy(x: 0, y: size.height)
  22. context.scaleBy(x: 1.0, y: -1.0)
  23. context.setBlendMode(.normal)
  24.  
  25. let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  26. context.clip(to: rect, mask: cgImage!)
  27.  
  28. newColor.setFill()
  29. context.fill(rect)
  30.  
  31. let newImage = UIGraphicsGetImageFromCurrentImageContext()!
  32. UIGraphicsEndImageContext()
  33. newImage.accessibilityIdentifier = accessibilityIdentifier
  34. return newImage
  35. }
  36.  
  37. if let accessibilityIdentifier = accessibilityIdentifier {
  38. return UIImage(imageName: accessibilityIdentifier)
  39. }
  40.  
  41. return self
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement