UIButton+Virgil.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // UIButton+Virgil.swift
  3. // DragonflySchool
  4. //
  5. // Created by virgil on 16/2/20.
  6. // Copyright © 2016年 virgil. All rights reserved.
  7. //
  8. import UIKit
  9. extension UIButton {
  10. func loadImage(imgUrl: String, defaultImage: String) {
  11. if imgUrl != "" {
  12. let url = URL(string: imgUrl)
  13. if url != nil {
  14. self.sd_setImage(with: url!, for: .normal, placeholderImage: UIImage(named: defaultImage) )
  15. } else {
  16. self.setImage(UIImage(named: defaultImage), for: .normal)
  17. }
  18. } else {
  19. self.setImage(UIImage(named: defaultImage), for: .normal)
  20. }
  21. }
  22. func loadImage(imgUrl: String, defaultImage: String, completedBlock:@escaping SDExternalCompletionBlock) {
  23. if imgUrl != "" {
  24. let url = URL(string: imgUrl)
  25. if url != nil {
  26. self.sd_setImage(with: url!, for: .normal) { (img, error, cacheType, url) in
  27. completedBlock(img, error, cacheType, url)
  28. }
  29. //self.sd_setImage(with: url!, for: .normal, placeholderImage: UIImage(named: defaultImage) )
  30. } else {
  31. self.setImage(UIImage(named: defaultImage), for: .normal)
  32. }
  33. } else {
  34. self.setImage(UIImage(named: defaultImage), for: .normal)
  35. }
  36. }
  37. func loadBackgroudImage(imgUrl: String, defaultImage: String) {
  38. if imgUrl != "" {
  39. let url = URL(string: imgUrl)
  40. if url != nil {
  41. self.sd_setBackgroundImage(with: url!, for: .normal, placeholderImage: UIImage(named: defaultImage) )
  42. } else {
  43. self.setBackgroudImage(name: defaultImage)
  44. }
  45. } else {
  46. self.setBackgroudImage(name: defaultImage)
  47. }
  48. }
  49. }