Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.93 KB | None | 0 0
  1. extension UIImage{
  2. func Noir() -> UIImage {
  3. let currentFilter = CIFilter(name: "CIPhotoEffectNoir")
  4. currentFilter!.setValue(CIImage(image: self), forKey: kCIInputImageKey)
  5. let output = currentFilter!.outputImage
  6. let context = CIContext(options: nil)
  7. let cgimg = context.createCGImage(output!,from: output!.extent)
  8. let processedImage = UIImage(cgImage: cgimg!)
  9. return processedImage
  10. }
  11. }
  12.  
  13. func computeImageToPrint() -> UIImage{
  14. class DescriptionView : UIView{
  15. let separatorView : UIView = {
  16. let frame : CGRect
  17. if EggMN.egg?.translations.first(where: {$0.idTranslation == EggMN.egg?.config?.current_language?.code})?.isRTL ?? false
  18. {
  19. frame = CGRect(x: 499, y: 20, width: 1, height: 460)
  20. }
  21. else{
  22. frame = CGRect(x: 0, y: 20, width: 1, height: 460)
  23. }
  24. let sv = UIView(frame: frame)
  25. sv.backgroundColor = UIColor.black
  26. return sv
  27. }()
  28. let titleLabel : UILabel = {
  29. let lb = UILabel(frame: CGRect(x: 0, y: 0, width: 460, height: 80))
  30. lb.font = UIFont.header4
  31. //lb.adjustsFontSizeToFitWidth = true
  32. lb.sizeToFit()
  33. lb.translatesAutoresizingMaskIntoConstraints = false
  34. return lb
  35. }()
  36. let descriptionLabel : UILabel = {
  37. let lb = UILabel(frame: CGRect(x: 0, y: 0, width: 460, height: 380))
  38. lb.font = UIFont.bodyCopy3
  39. lb.numberOfLines = 0
  40. lb.sizeToFit()
  41. lb.translatesAutoresizingMaskIntoConstraints = false
  42. return lb
  43. }()
  44. let mainConcernLabel : UILabel = {
  45. let lb = UILabel(frame: CGRect(x: 0, y: 0, width: 460, height: 80))
  46. lb.font = UIFont.header4
  47. lb.numberOfLines = 0
  48. lb.sizeToFit()
  49. lb.text = "\("CR-email-share.email-concern-label".ls())"
  50. lb.translatesAutoresizingMaskIntoConstraints = false
  51. return lb
  52. }()
  53. let skinTypeLabel : UILabel = {
  54. let lb = UILabel(frame: CGRect(x: 0, y: 0, width: 460, height: 80))
  55. lb.font = UIFont.header4
  56. lb.numberOfLines = 0
  57. lb.sizeToFit()
  58. lb.text = "CR-email-share.email-skin-type-label".ls()
  59. lb.translatesAutoresizingMaskIntoConstraints = false
  60. return lb
  61. }()
  62.  
  63. init(title: String, description: String, isFirstPage : Bool = false, skinType : String = ""){
  64. super.init(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
  65. addSubview(titleLabel)
  66. addSubview(descriptionLabel)
  67. addSubview(separatorView)
  68. self.titleLabel.text = title
  69. self.descriptionLabel.text = description
  70. titleLabel.widthAnchor.constraint(equalToConstant: 460).isActive = true
  71. titleLabel.heightAnchor.constraint(equalToConstant: 80).isActive = true
  72. titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  73. titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 20).isActive = true
  74. descriptionLabel.widthAnchor.constraint(equalToConstant: 460).isActive = true
  75. //descriptionLabel.heightAnchor.constraint(equalToConstant: 380).isActive = true
  76. descriptionLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  77. descriptionLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 50).isActive = true
  78.  
  79. if isFirstPage{
  80. addSubview(mainConcernLabel)
  81. addSubview(skinTypeLabel)
  82. mainConcernLabel.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: 50).isActive = true
  83. mainConcernLabel.widthAnchor.constraint(equalToConstant: 460).isActive = true
  84. mainConcernLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  85.  
  86. skinTypeLabel.topAnchor.constraint(equalTo: mainConcernLabel.bottomAnchor, constant: 50).isActive = true
  87. skinTypeLabel.widthAnchor.constraint(equalToConstant: 460).isActive = true
  88. skinTypeLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  89. }
  90. }
  91.  
  92. required init?(coder aDecoder: NSCoder) {
  93. fatalError("init(coder:) has not been implemented")
  94. }
  95. }
  96.  
  97. class ProductPrintView : UIView{
  98. let titleLabel : UILabel = {
  99. let lb = UILabel(frame: CGRect(x: 0, y: 0, width: 460, height: 120))
  100. lb.font = UIFont.header2
  101. lb.numberOfLines = 0
  102. lb.textColor = UIColor.gray_1
  103. lb.translatesAutoresizingMaskIntoConstraints = false
  104. return lb
  105. }()
  106. let illustrationView : UIImageView = {
  107. let iv = UIImageView(frame: CGRect(x: 0, y: 140, width: 460, height: 290))
  108. iv.contentMode = .scaleAspectFit
  109. iv.translatesAutoresizingMaskIntoConstraints = false
  110. return iv
  111. }()
  112. let checkBox : UIView = {
  113. let cb = UIView(frame: CGRect(x: 50, y: 430, width: 50, height: 50))
  114. cb.backgroundColor = UIColor.white
  115. cb.layer.borderWidth = 1
  116. cb.layer.borderColor = UIColor.gray_1.cgColor
  117. return cb
  118. }()
  119. let checkBoxLabel : UILabel = {
  120. let lb = UILabel(frame: CGRect(x: 90, y: 430, width: 360, height: 50))
  121. lb.translatesAutoresizingMaskIntoConstraints = false
  122. lb.font = UIFont.header3
  123. lb.textColor = UIColor.gray_1
  124. lb.text = "Coche la case"
  125. return lb
  126. }()
  127. let bottomStackView : UIStackView = {
  128. let sv = UIStackView()
  129. sv.axis = .horizontal
  130. sv.spacing = 20
  131. sv.translatesAutoresizingMaskIntoConstraints = false
  132. return sv
  133. }()
  134.  
  135. init(product: Product){
  136. super.init(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
  137. addSubview(titleLabel)
  138. addSubview(illustrationView)
  139. bottomStackView.addArrangedSubview(checkBox)
  140. bottomStackView.addArrangedSubview(checkBoxLabel)
  141. addSubview(bottomStackView)
  142. //addSubview(checkBox)
  143. //addSubview(checkBoxLabel)
  144. self.titleLabel.text = product.completeName()
  145. titleLabel.sizeToFit()
  146. var imagePath = EggMN.egg!.folder
  147. imagePath.appendPathComponent(product.imagePath)
  148. let image = UIImage(contentsOfFile: imagePath.path)
  149. self.illustrationView.image = image
  150. titleLabel.widthAnchor.constraint(equalToConstant: 460).isActive = true
  151. titleLabel.heightAnchor.constraint(equalToConstant: 120).isActive = true
  152. titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  153. titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 20).isActive = true
  154. checkBox.heightAnchor.constraint(equalToConstant: 50).isActive = true
  155. checkBox.widthAnchor.constraint(equalToConstant: 50).isActive = true
  156. //checkBox.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true
  157. //checkBox.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20).isActive = true
  158. illustrationView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor).isActive = true
  159. illustrationView.bottomAnchor.constraint(equalTo: checkBox.topAnchor).isActive = true
  160. illustrationView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  161. illustrationView.widthAnchor.constraint(equalToConstant: 460).isActive = true
  162. checkBoxLabel.heightAnchor.constraint(equalTo: checkBox.heightAnchor).isActive = true
  163. //checkBoxLabel.leftAnchor.constraint(equalTo: checkBox.rightAnchor, constant: 20).isActive = true
  164. //checkBoxLabel.topAnchor.constraint(equalTo: illustrationView.bottomAnchor).isActive = true
  165. checkBoxLabel.widthAnchor.constraint(equalToConstant: 360).isActive = true
  166. bottomStackView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20).isActive = true
  167. bottomStackView.widthAnchor.constraint(equalTo: widthAnchor, constant: -40).isActive = true
  168. bottomStackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  169. bottomStackView.heightAnchor.constraint(equalToConstant: 50).isActive = true
  170. }
  171.  
  172. required init?(coder aDecoder: NSCoder) {
  173. fatalError("init(coder:) has not been implemented")
  174. }
  175. }
  176.  
  177. let mainStackView = UIStackView()
  178. let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
  179. let logoView = UIImageView(image: #imageLiteral(resourceName: "img_logo_left"))
  180. logoView.translatesAutoresizingMaskIntoConstraints = false
  181. view.addSubview(logoView)
  182. logoView.widthAnchor.constraint(equalToConstant: 440).isActive = true
  183. logoView.heightAnchor.constraint(equalToConstant: 440).isActive = true
  184. logoView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
  185. logoView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
  186.  
  187. logoView.contentMode = .scaleAspectFit
  188. mainStackView.addArrangedSubview(view)
  189. //logoView.widthAnchor.constraint(equalToConstant: 460).isActive = true
  190.  
  191. let firstView = DescriptionView(title: "CR-print-template.print-template-hdr".ls(), description: "CR-print-template.print-template-copy".ls(), isFirstPage: true)
  192. firstView.mainConcernLabel.text?.append(contentsOf: " \(self.concern.getName())")
  193. firstView.skinTypeLabel.text?.append(contentsOf: " \(concernResult.getName())")
  194. mainStackView.addArrangedSubview(firstView)
  195. if productsMoisturizer.count > 0{
  196. mainStackView.addArrangedSubview(DescriptionView(title: "CR-three-up-product-view.product-moisturizer".ls(), description: "CR-three-up-product-view.product-moisturizer-desc".ls()))
  197. for prod in productsMoisturizer{
  198. mainStackView.addArrangedSubview(ProductPrintView(product: prod))
  199. }
  200. }
  201. if productsFoundation.count > 0{
  202. mainStackView.addArrangedSubview(DescriptionView(title: "CR-three-up-product-view.product-foundation".ls(), description: "CR-three-up-product-view.product-foundation-desc".ls()))
  203. for prod in productsFoundation{
  204. mainStackView.addArrangedSubview(ProductPrintView(product: prod))
  205. }
  206. }
  207.  
  208. if productsOtherProducts.count > 0{
  209. for categ in productsOtherProducts{
  210. mainStackView.addArrangedSubview(DescriptionView(title: "CR-three-up-product-view.\((categ.first?.category)!)".ls(), description: "CR-three-up-product-view.\((categ.first?.category)!)-desc".ls()))
  211.  
  212. for prod in categ{
  213. print(prod.category)
  214. mainStackView.addArrangedSubview(ProductPrintView(product: prod))
  215. }
  216. }
  217. }
  218.  
  219. mainStackView.axis = .horizontal
  220. mainStackView.distribution = .fillEqually
  221. mainStackView.translatesAutoresizingMaskIntoConstraints = false
  222.  
  223. let mainView = UIView(frame: CGRect(x: 0, y: 0, width: mainStackView.arrangedSubviews.count * 500, height: 500))
  224. mainView.backgroundColor = UIColor.white
  225.  
  226. mainView.addSubview(mainStackView)
  227.  
  228. mainStackView.widthAnchor.constraint(equalTo: mainView.widthAnchor).isActive = true
  229. mainStackView.heightAnchor.constraint(equalTo: mainView.heightAnchor).isActive = true
  230. mainStackView.centerXAnchor.constraint(equalTo: mainView.centerXAnchor).isActive = true
  231. mainStackView.centerYAnchor.constraint(equalTo: mainView.centerYAnchor).isActive = true
  232. let snap = mainView.snapshotView(afterScreenUpdates: true)
  233. UIGraphicsBeginImageContext(mainView.bounds.size)
  234. mainView.layer.render(in: UIGraphicsGetCurrentContext()!)
  235. //mainView.drawHierarchy(in: mainView.bounds, afterScreenUpdates: true)
  236. let imageToPrint = UIGraphicsGetImageFromCurrentImageContext()!
  237. UIGraphicsEndImageContext()
  238.  
  239. return imageToPrint.Noir()
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement