iOS 7 Core Image QR Code generation too blur

OMGPOP picture OMGPOP · Mar 13, 2014 · Viewed 14.1k times · Source

here's my code for generating QRCode image

+ (UIImage *)generateQRCodeWithString:(NSString *)string {
    NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setValue:stringData forKey:@"inputMessage"];
    [filter setValue:@"M" forKey:@"inputCorrectionLevel"];
    return [UIImage imageWithCIImage:filter.outputImage];
}

The result is too blur. Is it possible to set the size of the generated qr code?

Answer

Lope picture Lope · Jun 7, 2018

Easiest solution is to add following to your image view:

imgViewQR.layer.magnificationFilter = kCAFilterNearest

This will automatically upscale your generated QR code image to imageview's size using nearest which results in sharp, pixelated image. This usually isn't what you want when resizing icons/photos but is perfect for QR codes

enter image description here

(it doesn't seem to work on simulator but works great on real device