为什么UIImagePNGRepresentation()返回nil?当我尝试存储新图像时

时间:2017-03-17 03:04:35

标签: swift uiimage uiimagepngrepresentation

我正在生成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 !!!

0 个答案:

没有答案