LNOrderShareImageGenerator.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // LNOrderShareImageGenerator.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/11/27.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNOrderShareImageGenerator {
  11. private let container = UIImageView()
  12. private let avatar = UIImageView()
  13. private let userNameLabel = UILabel()
  14. private let scoreView = LNFiveStarScoreView()
  15. private let qrCodeView = UIImageView()
  16. private let costLabel = UILabel()
  17. private let skillIc = UIImageView()
  18. private let skillNameLabel = UILabel()
  19. private let countLabel = UILabel()
  20. init() {
  21. setupViews()
  22. }
  23. func update(skill: LNGameMateSkillVO, count: Int, image: UIImage) {
  24. avatar.showAvatar(myUserInfo.avatar)
  25. skillIc.sd_setImage(with: URL(string: skill.icon))
  26. skillNameLabel.text = skill.name
  27. scoreView.score = myUserInfo.star
  28. qrCodeView.image = image
  29. countLabel.text = .init(key: "A00162", count, skill.unit)
  30. let cost = skill.price * Double(count)
  31. costLabel.text = cost.toDisplay
  32. container.layoutIfNeeded()
  33. }
  34. func generate() -> UIImage {
  35. let renderer = UIGraphicsImageRenderer(bounds: container.bounds)
  36. return renderer.image { _ in
  37. container.drawHierarchy(
  38. in: container.bounds,
  39. afterScreenUpdates: true
  40. )
  41. }
  42. }
  43. }
  44. extension LNOrderShareImageGenerator {
  45. private func setupViews() {
  46. let image: UIImage = .icOrderShareBg
  47. container.image = image
  48. container.frame = .init(x: 0, y: 0, width: image.size.width, height: image.size.height)
  49. avatar.backgroundColor = .fill
  50. avatar.layer.cornerRadius = 37.5
  51. avatar.layer.borderWidth = 2
  52. avatar.layer.borderColor = UIColor.fill.cgColor
  53. avatar.clipsToBounds = true
  54. avatar.showAvatar(myUserInfo.avatar)
  55. container.addSubview(avatar)
  56. avatar.snp.makeConstraints { make in
  57. make.centerX.equalToSuperview()
  58. make.top.equalToSuperview().offset(30)
  59. make.width.height.equalTo(75)
  60. }
  61. userNameLabel.font = .heading_h2
  62. userNameLabel.textColor = .text_5
  63. userNameLabel.text = myUserInfo.nickname
  64. container.addSubview(userNameLabel)
  65. userNameLabel.snp.makeConstraints { make in
  66. make.centerX.equalToSuperview()
  67. make.top.equalTo(avatar.snp.bottom).offset(2)
  68. }
  69. scoreView.icSize = 10
  70. scoreView.spacing = 4
  71. container.addSubview(scoreView)
  72. scoreView.snp.makeConstraints { make in
  73. make.centerX.equalToSuperview()
  74. make.top.equalTo(userNameLabel.snp.bottom).offset(2)
  75. }
  76. container.addSubview(qrCodeView)
  77. qrCodeView.snp.makeConstraints { make in
  78. make.centerX.equalToSuperview()
  79. make.top.equalTo(scoreView.snp.bottom).offset(13)
  80. make.width.height.equalTo(145)
  81. }
  82. let priceView = UIView()
  83. container.addSubview(priceView)
  84. priceView.snp.makeConstraints { make in
  85. make.centerX.equalToSuperview()
  86. make.top.equalTo(qrCodeView.snp.bottom).offset(6)
  87. }
  88. let coin = UIImageView.coinImageView()
  89. priceView.addSubview(coin)
  90. coin.snp.makeConstraints { make in
  91. make.centerY.equalToSuperview()
  92. make.leading.equalToSuperview()
  93. make.width.height.equalTo(20)
  94. }
  95. costLabel.font = .heading_h2
  96. costLabel.textColor = .text_5
  97. priceView.addSubview(costLabel)
  98. costLabel.snp.makeConstraints { make in
  99. make.verticalEdges.equalToSuperview()
  100. make.trailing.equalToSuperview()
  101. make.leading.equalTo(coin.snp.trailing).offset(2)
  102. }
  103. let tipsView = UIImageView()
  104. tipsView.image = .primary_7
  105. tipsView.layer.cornerRadius = 14.5
  106. tipsView.clipsToBounds = true
  107. container.addSubview(tipsView)
  108. tipsView.snp.makeConstraints { make in
  109. make.centerX.equalToSuperview()
  110. make.top.equalTo(priceView.snp.bottom).offset(9)
  111. }
  112. let tipsLabel = UILabel()
  113. tipsLabel.text = .init(key: "A00164")
  114. tipsLabel.font = .heading_h4
  115. tipsLabel.textColor = .text_1
  116. tipsView.addSubview(tipsLabel)
  117. tipsLabel.snp.makeConstraints { make in
  118. make.verticalEdges.equalToSuperview().inset(6)
  119. make.horizontalEdges.equalToSuperview().inset(21)
  120. }
  121. skillIc.layer.cornerRadius = 10
  122. skillIc.clipsToBounds = true
  123. container.addSubview(skillIc)
  124. skillIc.snp.makeConstraints { make in
  125. make.leading.equalToSuperview().offset(56)
  126. make.bottom.equalToSuperview().offset(-88)
  127. make.width.height.equalTo(20)
  128. }
  129. skillNameLabel.font = .heading_h5
  130. skillNameLabel.textColor = .text_5
  131. container.addSubview(skillNameLabel)
  132. skillNameLabel.snp.makeConstraints { make in
  133. make.centerY.equalTo(skillIc)
  134. make.leading.equalTo(skillIc.snp.trailing).offset(8)
  135. }
  136. countLabel.font = .body_m
  137. countLabel.textColor = .text_5
  138. container.addSubview(countLabel)
  139. countLabel.snp.makeConstraints { make in
  140. make.centerY.equalTo(skillIc)
  141. make.trailing.equalToSuperview().offset(-58)
  142. }
  143. }
  144. }