Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension UIImage{
- func Noir() -> UIImage {
- let currentFilter = CIFilter(name: "CIPhotoEffectNoir")
- currentFilter!.setValue(CIImage(image: self), forKey: kCIInputImageKey)
- let output = currentFilter!.outputImage
- let context = CIContext(options: nil)
- let cgimg = context.createCGImage(output!,from: output!.extent)
- let processedImage = UIImage(cgImage: cgimg!)
- return processedImage
- }
- }
- func computeImageToPrint() -> UIImage{
- class DescriptionView : UIView{
- let separatorView : UIView = {
- let frame : CGRect
- if EggMN.egg?.translations.first(where: {$0.idTranslation == EggMN.egg?.config?.current_language?.code})?.isRTL ?? false
- {
- frame = CGRect(x: 499, y: 20, width: 1, height: 460)
- }
- else{
- frame = CGRect(x: 0, y: 20, width: 1, height: 460)
- }
- let sv = UIView(frame: frame)
- sv.backgroundColor = UIColor.black
- return sv
- }()
- let titleLabel : UILabel = {
- let lb = UILabel(frame: CGRect(x: 0, y: 0, width: 460, height: 80))
- lb.font = UIFont.header4
- //lb.adjustsFontSizeToFitWidth = true
- lb.sizeToFit()
- lb.translatesAutoresizingMaskIntoConstraints = false
- return lb
- }()
- let descriptionLabel : UILabel = {
- let lb = UILabel(frame: CGRect(x: 0, y: 0, width: 460, height: 380))
- lb.font = UIFont.bodyCopy3
- lb.numberOfLines = 0
- lb.sizeToFit()
- lb.translatesAutoresizingMaskIntoConstraints = false
- return lb
- }()
- let mainConcernLabel : UILabel = {
- let lb = UILabel(frame: CGRect(x: 0, y: 0, width: 460, height: 80))
- lb.font = UIFont.header4
- lb.numberOfLines = 0
- lb.sizeToFit()
- lb.text = "\("CR-email-share.email-concern-label".ls())"
- lb.translatesAutoresizingMaskIntoConstraints = false
- return lb
- }()
- let skinTypeLabel : UILabel = {
- let lb = UILabel(frame: CGRect(x: 0, y: 0, width: 460, height: 80))
- lb.font = UIFont.header4
- lb.numberOfLines = 0
- lb.sizeToFit()
- lb.text = "CR-email-share.email-skin-type-label".ls()
- lb.translatesAutoresizingMaskIntoConstraints = false
- return lb
- }()
- init(title: String, description: String, isFirstPage : Bool = false, skinType : String = ""){
- super.init(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
- addSubview(titleLabel)
- addSubview(descriptionLabel)
- addSubview(separatorView)
- self.titleLabel.text = title
- self.descriptionLabel.text = description
- titleLabel.widthAnchor.constraint(equalToConstant: 460).isActive = true
- titleLabel.heightAnchor.constraint(equalToConstant: 80).isActive = true
- titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
- titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 20).isActive = true
- descriptionLabel.widthAnchor.constraint(equalToConstant: 460).isActive = true
- //descriptionLabel.heightAnchor.constraint(equalToConstant: 380).isActive = true
- descriptionLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
- descriptionLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 50).isActive = true
- if isFirstPage{
- addSubview(mainConcernLabel)
- addSubview(skinTypeLabel)
- mainConcernLabel.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: 50).isActive = true
- mainConcernLabel.widthAnchor.constraint(equalToConstant: 460).isActive = true
- mainConcernLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
- skinTypeLabel.topAnchor.constraint(equalTo: mainConcernLabel.bottomAnchor, constant: 50).isActive = true
- skinTypeLabel.widthAnchor.constraint(equalToConstant: 460).isActive = true
- skinTypeLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
- }
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
- class ProductPrintView : UIView{
- let titleLabel : UILabel = {
- let lb = UILabel(frame: CGRect(x: 0, y: 0, width: 460, height: 120))
- lb.font = UIFont.header2
- lb.numberOfLines = 0
- lb.textColor = UIColor.gray_1
- lb.translatesAutoresizingMaskIntoConstraints = false
- return lb
- }()
- let illustrationView : UIImageView = {
- let iv = UIImageView(frame: CGRect(x: 0, y: 140, width: 460, height: 290))
- iv.contentMode = .scaleAspectFit
- iv.translatesAutoresizingMaskIntoConstraints = false
- return iv
- }()
- let checkBox : UIView = {
- let cb = UIView(frame: CGRect(x: 50, y: 430, width: 50, height: 50))
- cb.backgroundColor = UIColor.white
- cb.layer.borderWidth = 1
- cb.layer.borderColor = UIColor.gray_1.cgColor
- return cb
- }()
- let checkBoxLabel : UILabel = {
- let lb = UILabel(frame: CGRect(x: 90, y: 430, width: 360, height: 50))
- lb.translatesAutoresizingMaskIntoConstraints = false
- lb.font = UIFont.header3
- lb.textColor = UIColor.gray_1
- lb.text = "Coche la case"
- return lb
- }()
- let bottomStackView : UIStackView = {
- let sv = UIStackView()
- sv.axis = .horizontal
- sv.spacing = 20
- sv.translatesAutoresizingMaskIntoConstraints = false
- return sv
- }()
- init(product: Product){
- super.init(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
- addSubview(titleLabel)
- addSubview(illustrationView)
- bottomStackView.addArrangedSubview(checkBox)
- bottomStackView.addArrangedSubview(checkBoxLabel)
- addSubview(bottomStackView)
- //addSubview(checkBox)
- //addSubview(checkBoxLabel)
- self.titleLabel.text = product.completeName()
- titleLabel.sizeToFit()
- var imagePath = EggMN.egg!.folder
- imagePath.appendPathComponent(product.imagePath)
- let image = UIImage(contentsOfFile: imagePath.path)
- self.illustrationView.image = image
- titleLabel.widthAnchor.constraint(equalToConstant: 460).isActive = true
- titleLabel.heightAnchor.constraint(equalToConstant: 120).isActive = true
- titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
- titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 20).isActive = true
- checkBox.heightAnchor.constraint(equalToConstant: 50).isActive = true
- checkBox.widthAnchor.constraint(equalToConstant: 50).isActive = true
- //checkBox.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true
- //checkBox.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20).isActive = true
- illustrationView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor).isActive = true
- illustrationView.bottomAnchor.constraint(equalTo: checkBox.topAnchor).isActive = true
- illustrationView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
- illustrationView.widthAnchor.constraint(equalToConstant: 460).isActive = true
- checkBoxLabel.heightAnchor.constraint(equalTo: checkBox.heightAnchor).isActive = true
- //checkBoxLabel.leftAnchor.constraint(equalTo: checkBox.rightAnchor, constant: 20).isActive = true
- //checkBoxLabel.topAnchor.constraint(equalTo: illustrationView.bottomAnchor).isActive = true
- checkBoxLabel.widthAnchor.constraint(equalToConstant: 360).isActive = true
- bottomStackView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20).isActive = true
- bottomStackView.widthAnchor.constraint(equalTo: widthAnchor, constant: -40).isActive = true
- bottomStackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
- bottomStackView.heightAnchor.constraint(equalToConstant: 50).isActive = true
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
- let mainStackView = UIStackView()
- let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
- let logoView = UIImageView(image: #imageLiteral(resourceName: "img_logo_left"))
- logoView.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(logoView)
- logoView.widthAnchor.constraint(equalToConstant: 440).isActive = true
- logoView.heightAnchor.constraint(equalToConstant: 440).isActive = true
- logoView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
- logoView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
- logoView.contentMode = .scaleAspectFit
- mainStackView.addArrangedSubview(view)
- //logoView.widthAnchor.constraint(equalToConstant: 460).isActive = true
- let firstView = DescriptionView(title: "CR-print-template.print-template-hdr".ls(), description: "CR-print-template.print-template-copy".ls(), isFirstPage: true)
- firstView.mainConcernLabel.text?.append(contentsOf: " \(self.concern.getName())")
- firstView.skinTypeLabel.text?.append(contentsOf: " \(concernResult.getName())")
- mainStackView.addArrangedSubview(firstView)
- if productsMoisturizer.count > 0{
- mainStackView.addArrangedSubview(DescriptionView(title: "CR-three-up-product-view.product-moisturizer".ls(), description: "CR-three-up-product-view.product-moisturizer-desc".ls()))
- for prod in productsMoisturizer{
- mainStackView.addArrangedSubview(ProductPrintView(product: prod))
- }
- }
- if productsFoundation.count > 0{
- mainStackView.addArrangedSubview(DescriptionView(title: "CR-three-up-product-view.product-foundation".ls(), description: "CR-three-up-product-view.product-foundation-desc".ls()))
- for prod in productsFoundation{
- mainStackView.addArrangedSubview(ProductPrintView(product: prod))
- }
- }
- if productsOtherProducts.count > 0{
- for categ in productsOtherProducts{
- mainStackView.addArrangedSubview(DescriptionView(title: "CR-three-up-product-view.\((categ.first?.category)!)".ls(), description: "CR-three-up-product-view.\((categ.first?.category)!)-desc".ls()))
- for prod in categ{
- print(prod.category)
- mainStackView.addArrangedSubview(ProductPrintView(product: prod))
- }
- }
- }
- mainStackView.axis = .horizontal
- mainStackView.distribution = .fillEqually
- mainStackView.translatesAutoresizingMaskIntoConstraints = false
- let mainView = UIView(frame: CGRect(x: 0, y: 0, width: mainStackView.arrangedSubviews.count * 500, height: 500))
- mainView.backgroundColor = UIColor.white
- mainView.addSubview(mainStackView)
- mainStackView.widthAnchor.constraint(equalTo: mainView.widthAnchor).isActive = true
- mainStackView.heightAnchor.constraint(equalTo: mainView.heightAnchor).isActive = true
- mainStackView.centerXAnchor.constraint(equalTo: mainView.centerXAnchor).isActive = true
- mainStackView.centerYAnchor.constraint(equalTo: mainView.centerYAnchor).isActive = true
- let snap = mainView.snapshotView(afterScreenUpdates: true)
- UIGraphicsBeginImageContext(mainView.bounds.size)
- mainView.layer.render(in: UIGraphicsGetCurrentContext()!)
- //mainView.drawHierarchy(in: mainView.bounds, afterScreenUpdates: true)
- let imageToPrint = UIGraphicsGetImageFromCurrentImageContext()!
- UIGraphicsEndImageContext()
- return imageToPrint.Noir()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement