gpt4 book ai didi

ios - 条形码未生成

转载 作者:行者123 更新时间:2023-11-30 11:32:35 25 4
gpt4 key购买 nike

我正在使用 link 中的以下代码从字符串生成 Code128 条形码。由于某种原因它没有醒来。该函数确实返回图像,但在将图像分配给 UIImageView 后,我无法在模拟器中看到它。

 class Barcode {

class func fromString(string : String) -> UIImage? {

let data = string.data(using: .ascii)
let filter = CIFilter(name: "CICode128BarcodeGenerator")
filter?.setValue(data, forKey: "inputMessage")

return UIImage(ciImage: (filter?.outputImage)!)
}

}

已导入CoreImage。函数调用:

let barcodeImg = Barcode.fromString(string: number)
self.userBarcodeImageView.image = barcodeImg

谁能告诉我问题是什么?

已解决

替换了此函数的代码,可在 link 中找到

func generateBarcode(from string: String) -> UIImage? {

let data = string.data(using: String.Encoding.ascii)

if let filter = CIFilter(name: "CICode128BarcodeGenerator") {
filter.setDefaults()
//Margin
filter.setValue(7.00, forKey: "inputQuietSpace")
filter.setValue(data, forKey: "inputMessage")
//Scaling
let transform = CGAffineTransform(scaleX: 3, y: 3)

if let output = filter.outputImage?.applying(transform) {
let context:CIContext = CIContext.init(options: nil)
let cgImage:CGImage = context.createCGImage(output, from: output.extent)!
let rawImage:UIImage = UIImage.init(cgImage: cgImage)

//Refinement code to allow conversion to NSData or share UIImage. Code here:
//http://stackoverflow.com/questions/2240395/uiimage-created-from-cgimageref-fails-with-uiimagepngrepresentation
let cgimage: CGImage = (rawImage.cgImage)!
let cropZone = CGRect(x: 0, y: 0, width: Int(rawImage.size.width), height: Int(rawImage.size.height))
let cWidth: size_t = size_t(cropZone.size.width)
let cHeight: size_t = size_t(cropZone.size.height)
let bitsPerComponent: size_t = cgimage.bitsPerComponent
//THE OPERATIONS ORDER COULD BE FLIPPED, ALTHOUGH, IT DOESN'T AFFECT THE RESULT
let bytesPerRow = (cgimage.bytesPerRow) / (cgimage.width * cWidth)

let context2: CGContext = CGContext(data: nil, width: cWidth, height: cHeight, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: cgimage.bitmapInfo.rawValue)!

context2.draw(cgimage, in: cropZone)

let result: CGImage = context2.makeImage()!
let finalImage = UIImage(cgImage: result)

return finalImage

}
}

return nil
}

开始工作了。

最佳答案

我也遇到过同样的问题。经过长期研究找到以下解决方案。

请确保您的imageView高度和宽度应为整数。如果imageView高度和宽度是 float ,那么它不会生成条形码图像。

关于ios - 条形码未生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50101189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com