Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index out of range when printing image #35

Open
MrGD0924 opened this issue May 4, 2020 · 5 comments
Open

Index out of range when printing image #35

MrGD0924 opened this issue May 4, 2020 · 5 comments

Comments

@MrGD0924
Copy link

MrGD0924 commented May 4, 2020

how to fix it? Please give me response....

@MrGD0924
Copy link
Author

MrGD0924 commented May 4, 2020

import Printer

class ViewController: UIViewController {

let pm = PrinterManager()
let a = "3025856504282187562209314690839033975462381239936941770344840034144563413477955812966608888478861688170367239204693672108967226008937498861858661874577081699385849994003420752137009590997581532249708930640325165906723111760564674259827980794930442717664614029549142170395528352178494728525469158037062010011689105259"

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func touchPrint(sender: UIButton) {

    let image = generateQRCode(from: a)
    if pm.canPrint {

        var receipt = Receipt(
            .dividing,
            .image(image!, attributes: [ImageBlock.PredefinedAttribute.alignment(.center)]),
            //  Updated by Pradeep Sakharelia on 15/05/19
            .dividing
        )
        
        receipt.feedLinesOnTail = 2
        receipt.feedPointsPerLine = 60
        
        pm.print(receipt)
    } else {

        performSegue(withIdentifier: "ShowSelectPrintVC", sender: nil)
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let vc = segue.destination as? PrinterTableViewController {
        vc.sectionTitle = "Choose Printer"
        vc.printerManager = pm
    }
}

func generateQRCode(from string: String) -> UIImage? {
   // Get data from the string
    let data = string.data(using: String.Encoding.ascii)
    // Get a QR CIFilter
    guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil}
    // Input the data
    qrFilter.setValue(data, forKey: "inputMessage")
    // Get the output image
    guard let qrImage = qrFilter.outputImage else { return nil}
    // Scale the image
    let transform = CGAffineTransform(scaleX: 10, y: 10)
    let scaledQrImage = qrImage.transformed(by: transform)
    // Invert the colors
    guard let colorInvertFilter = CIFilter(name: "CIColorInvert") else { return nil}
    colorInvertFilter.setValue(scaledQrImage, forKey: "inputImage")
    guard let outputInvertedImage = colorInvertFilter.outputImage else { return nil}
    // Replace the black with transparency
    guard let maskToAlphaFilter = CIFilter(name: "CIMaskToAlpha") else { return nil}
    maskToAlphaFilter.setValue(outputInvertedImage, forKey: "inputImage")
    guard let outputCIImage = maskToAlphaFilter.outputImage else { return nil}
    // Do some processing to get the UIImage
    let context = CIContext()
    guard let cgImage = context.createCGImage(outputCIImage, from: outputCIImage.extent) else { return nil}
    return UIImage(cgImage: cgImage)
}

}

@AlekseyPakAA
Copy link

AlekseyPakAA commented Jul 8, 2020

Try to create an image where width % 8 == 0. I think there's a bug related to rounding.

@ramniwaseasyeat
Copy link

Any solution on this, I'm also facing same problem

@chibombo
Copy link
Contributor

Actually, the develop branch has a TODO that says "Make sure Image you want to print with esc/pos is square, like 32x32, 64x64, or 128x128. I have modified Printer project as well as Example in pull request. Hope it prints well."

@KevinGong2013
Copy link
Owner

Image command has rewrite.
please test with latest code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants