Home > OS >  Why is QR code displayed in swiftui extremely blurry as size increases?
Why is QR code displayed in swiftui extremely blurry as size increases?

Time:11-09

I am trying to display a QR code with SwiftUI but as soon as the size of the QR code increases, it becomes extremely blurry. Also when I navigate to QRcode view there is a slight delay in the screen being presented, not sure if it's a performance issue.

I get a log saying

The filter 'CIPortraitEffectSpillCorrection' is not implemented in the bundle at /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/CoreImage/PortraitFilters.cifilter.

Below is my code on how I implemented QR code view with SwiftUI

struct SampleView: View {
    
    let qrWidth = UIScreen.screenWidth / 2
    
    let context = CIContext()
    let filter = CIFilter.qrCodeGenerator()
    
    func generateQRCode(from string: String) -> UIImage {
        filter.message = Data(string.utf8)
        
        if let outputImage = filter.outputImage {
            if let cgimg = context.createCGImage(outputImage, from: outputImage.extent) {
            return UIImage(cgImage: cgimg)
            }
        }
        
        return UIImage(systemName: "xmark.circle") ?? UIImage()
    }
    
    var body: some View {
        stack(alignment: .leading){
            if let qrImage = generateQRCode(from: "https://www.hackingwithswift.com/books/ios-swiftui/generating-and-scaling-up-a-qr-code") {
                Image(uiImage: qrImage)
                    .resizable()
                    .scaledToFit()
                    .frame(width: qrWidth,height: qrWidth)
            } else {
                Text("Error")
            }
        }.frame(maxWidth: .infinity,maxHeight: .infinity,alignment: .top)
            .padding().background(Constants.WHITE_COLOR)
    }
}

CodePudding user response:

Try adding

.interpolation(.none)

to the Image.

  • Related