Swift getGraphicsUtil().print() - no printing

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!

Jun Meng
I tried false for

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

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Anonymous (not verified)
The correct value for

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

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Jun Meng
I copied your exact code into

I copied your exact code into the same project on my MacBook. When I ran the app on iPhone and tried to print signature, I only saw black box in the signature area.

Then I changed the stroke color to yellow. Now I can see the printed signature with black background:

          image1.jpeg

Did I miss anything in code?

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Jun Meng
Attached is the project that

Attached is the project that prints signature with black background.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Anonymous (not verified)
Attached is the full version

Attached is the full version of the Xcode project that prints the signature normally. It was a modified version based on the Xcode project you posted at the beginning of this thread.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Jun Meng
I see what's wrong on my side

I see what's wrong on my side: the background color was not set in storyboard. With your code change, I can print signature now.

Thanks a lot!

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Anonymous (not verified)
Please use false for

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)

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Log in to post comments