Swift

Swift,第1张

Swift

You have a couple of issues in your pre. You need to convert your string to
data using String Encoding isoLatin1 before passing it to the filter. Another
issue is that to convert your CIImage to data you need to redraw/render your
CIImage and to prevent blurring the image when scaled you need to apply a
transform to the image to increase its size:

extension StringProtocol {    var qrCode: UIImage? {        guard let data = data(using: .isoLatin1), let outputImage = CIFilter(name: "CIQRCodeGenerator",        parameters: ["inputMessage": data, "inputCorrectionLevel": "M"])?.outputImage        else { return nil }        let size = outputImage.extent.integral        let output = CGSize(width: 250, height: 250)        let format = UIGraphicsImageRendererFormat()        format.scale = UIScreen.main.scale        return UIGraphicsImageRenderer(size: output, format: format).image { _ in outputImage .transformed(by: .init(scaleX: output.width/size.width, y: output.height/size.height)) .image .draw(in: .init(origin: .zero, size: output))        }    }}extension CIImage {    var image: UIImage { .init(ciImage: self) }}

Playground testing:

let link = "https://stackoverflow.com/questions/51178573/swift-image-data-from-ciimage-qr-pre-how-to-render-cifilter-output?noredirect=1"let image = link.qrCode!let data =  image.jpegData(compressionQuality: 1)  // 154785 bytes



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4909418.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-12
下一篇2022-11-12

发表评论

登录后才能评论

评论列表(0条)

    保存