here\'s my code for generating QRCode image
+ (UIImage *)generateQRCodeWithString:(NSString *)string {
NSData *stringData = [string dataUsingEncoding:NSU
I ran into the same problem, based on this tutorial this is how i fixed it :
-(UIImage *) generateQRCodeWithString:(NSString *)string scale:(CGFloat) scale{
NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding ];
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setValue:stringData forKey:@"inputMessage"];
[filter setValue:@"M" forKey:@"inputCorrectionLevel"];
// Render the image into a CoreGraphics image
CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:[filter outputImage] fromRect:[[filter outputImage] extent]];
//Scale the image usign CoreGraphics
UIGraphicsBeginImageContext(CGSizeMake([[filter outputImage] extent].size.width * scale, [filter outputImage].extent.size.width * scale));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
UIImage *preImage = UIGraphicsGetImageFromCurrentImageContext();
//Cleaning up .
UIGraphicsEndImageContext();
CGImageRelease(cgImage);
// Rotate the image
UIImage *qrImage = [UIImage imageWithCGImage:[preImage CGImage]
scale:[preImage scale]
orientation:UIImageOrientationDownMirrored];
return qrImage;
}
I had to adapt @cromanelli's answer to achieve perfect sharpness :
func convertTextToQRCode(text: String, withSize size: CGSize) -> UIImage {
let data = text.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")!
filter.setValue(data, forKey: "inputMessage")
filter.setValue("L", forKey: "inputCorrectionLevel")
var qrcodeCIImage = filter.outputImage!
let cgImage = CIContext(options:nil).createCGImage(qrcodeCIImage, fromRect: qrcodeCIImage.extent)
UIGraphicsBeginImageContext(CGSizeMake(size.width * UIScreen.mainScreen().scale, size.height * UIScreen.mainScreen().scale))
let context = UIGraphicsGetCurrentContext()
CGContextSetInterpolationQuality(context, .None)
CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage)
let preImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let qrCodeImage = UIImage(CGImage: preImage.CGImage!, scale: 1.0/UIScreen.mainScreen().scale, orientation: .DownMirrored)
return qrCodeImage
}