我正在生成QR码的UIImage,我想存储它。但是当UIImagePNGRepresentation将UIImage转换为Data时,它总是返回nil。请帮帮我!
public func generaImagenQR(texto:String)->UIImage?{
let data = texto.data(using: String.Encoding.ascii)
//Generate a QR code
if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 3, y: 3)
if let output = filter.outputImage?.applying(transform) {
return UIImage(ciImage: output)
}
}
return nil
}
let imagenDeQR = generaImagenQR(texto: "hola")
let data = UIImagePNGRepresentation(imagenDeQR!)
// data == nil !!!