7 Replies Latest reply on Apr 13, 2018 8:51 AM by Jun Meng

    Swift getGraphicsUtil().print() - no printing

    Jun Meng

      Hi,

       

      I am using Swift 4 to write iOS native app and try to print signature on QLn320 mobile printer. The downloaded sample code in SDK works fine, but my code does not print signature at all.

       

      I attached my Xcode Swift project. Could you please help me to solve the printing issue?

       

      Thanks a lot in advance!

        • Re: Swift getGraphicsUtil().print() - no printing
          Steven Si

          Please use false for andIsInsideFormat: parameter, as we want to print out the signature as image independently.

          try printer?.getGraphicsUtil().print (signatureImage!.cgImage!, atX: 0, atY: 0, withWidth: -1, withHeight: -1, andIsInsideFormat: false)
          
          • Re: Swift getGraphicsUtil().print() - no printing
            Jun Meng

            I tried false for andIsInsideFormat parameter. The signature still cannot be printed...

              • Re: Swift getGraphicsUtil().print() - no printing
                Steven Si

                The correct value for andIsInsideFormat is false in this case. I was able to see a blackbox got printed on the label, which means the API works. We may need to look into the code of signature capture part to see why we got a blackbox instead of the signature.

                 

                Update on 4/9/2018:

                Today, I looked a little further into the code and made a few changes in getSignature() function in YPDrawSignatureView.swift. Now, the getSignature() is as below, by commenting out the part of stroke part (without redraw) , specifying the opaque with the current view's opaque value and using 0.0 for scaling parameter.

                 

                    // Save the Signature as an UIImage
                    public func getSignature(scale:CGFloat = 1) -> UIImage? {
                        if !doesContainSignature { return nil }
                        UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.isOpaque, 0.0)
                        self.drawHierarchy(in: self.bounds, afterScreenUpdates: false)
                
                //        self.strokeColor.setStroke()
                //        self.path.stroke()
                
                        let signature = UIGraphicsGetImageFromCurrentImageContext()!
                        UIGraphicsEndImageContext()
                        
                        return signature
                
                    }
                

                 

                Then, in printTransfer() function, we directly call getSignature(), i.e. bypassing getCroppedSignature() to simplify the debug.

                 

                    // Print transfer with signature on mobile printer
                    @IBAction func printTransfer(_ sender: UIButton) {
                        let printer = ZebraPrinterUtil()
                        
                        if printer.serialNumber.isEmpty {
                            let alert = UIAlertController(title: "Message", message: "No printer is available", preferredStyle: .alert)
                            alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: nil))
                            self.present(alert, animated: true, completion: nil)
                            
                            return
                        }
                        
                        // Display warning message
                        let alert = UIAlertController(title: "Print Transfer", message: printer.warningMessageBeforePrintContinuous, preferredStyle: .actionSheet)
                        
                        let OKAction = UIAlertAction(title: "Ok", style: .default) { (_) in
                //            printer.printNewEvidenceTransfer(agencyName: self.agencyTextField.text!, contactPhone: self.phoneTextField.text!, personName: self.agencyNameTextField.text!, signatureImage: self.signatureView.getCroppedSignature())
                            
                            printer.printNewEvidenceTransfer(agencyName: self.agencyTextField.text!, contactPhone: self.phoneTextField.text!, personName: self.agencyNameTextField.text!, signatureImage: self.signatureView.getSignature())
                        }
                        
                        let cancelAction = UIAlertAction(title: "Cancel", style: .default)
                        
                        alert.addAction(OKAction)
                        alert.addAction(cancelAction)
                        
                        present(alert, animated: true)
                        
                    }
                

                With these changes, we can successfully print out the signature as show in the screenshot below. Hope this helps.

                IMG_0305.jpg