ImageUtil.swift 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. //
  2. // ImageUtil.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/8/30.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import Foundation
  9. extension UIImage{
  10. public static func base64ToImage(_ source:String,defaultImage:UIImage=UIImage(named: "personDefaultIcon")!) -> UIImage {
  11. if source != ""{
  12. let theImage = UIImage(data: Data(base64Encoded:source,options:NSData.Base64DecodingOptions.ignoreUnknownCharacters)!)
  13. return theImage!
  14. }else{
  15. return defaultImage
  16. }
  17. }
  18. class func image(color: UIColor, size: CGSize) -> UIImage {
  19. let rect = CGRect.init(x: 0, y: 0, width: size.width, height: size.height)
  20. UIGraphicsBeginImageContext(rect.size)
  21. let context = UIGraphicsGetCurrentContext()
  22. context?.setFillColor(color.cgColor)
  23. context?.fill(rect)
  24. let img = UIGraphicsGetImageFromCurrentImageContext()
  25. UIGraphicsEndImageContext()
  26. return img!
  27. }
  28. }