本文介绍了迅速生成一个二维码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图生成一个二维码,但是有错误
I tried to generate a qrcode, but it has error
let myString = "ggigiuui"
let data = myString.data(using: .ascii, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter?.setValue(data, forKey: "inputMessage")
let img = UIImage(ciImage: (filter?.outputImage)!)
qponImage.image = img
推荐答案
我使用了以下代码,并且运行良好.self.imgQRCode是要在其上显示QR的图像视图.
I have used the following code, and it is working perfectly.Where self.imgQRCode is the imageview on which you want to display QR.
func generateQRCode(from string: String) -> UIImage?
{
let data = string.data(using: String.Encoding.ascii)
if let filter = CIFilter(name: "CIQRCodeGenerator")
{
filter.setValue(data, forKey: "inputMessage")
guard let qrImage = filter.outputImage else {return nil}
let scaleX = self.imgQRCode.frame.size.width / qrImage.extent.size.width
let scaleY = self.imgQRCode.frame.size.height / qrImage.extent.size.height
let transform = CGAffineTransform(scaleX: scaleX, y: scaleY)
if let output = filter.outputImage?.transformed(by: transform)
{
return UIImage(ciImage: output)
}
}
return nil
}
这篇关于迅速生成一个二维码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!