Skip to content

Instantly share code, notes, and snippets.

@snej
Created September 21, 2015 16:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save snej/c210cc4cbfe8fd277186 to your computer and use it in GitHub Desktop.
Save snej/c210cc4cbfe8fd277186 to your computer and use it in GitHub Desktop.
Shows how to generate a QRCode on iOS or Mac OS X
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
typedef UIImage QRImage;
#else
#import <AppKit/AppKit.h>
typedef NSImage QRImage;
#endif
+ (QRImage*) QRCodeImageWithData: (NSData*)data size: (CGFloat)size {
CIFilter* filter = [CIFilter filterWithName: @"CIQRCodeGenerator"];
[filter setValue: data forKey: @"inputMessage"];
CIImage* ciImage = filter.outputImage;
if (!ciImage)
return nil;
#if TARGET_OS_IPHONE
UIImage* tinyImage = [[UIImage alloc] initWithCIImage: ciImage];
if (size <= tinyImage.size.width)
return tinyImage;
// Scale image up:
UIGraphicsBeginImageContext(CGSizeMake(size, size));
CGContextSetInterpolationQuality(UIGraphicsGetCurrentContext(), kCGInterpolationNone);
[tinyImage drawInRect: CGRectMake(0, 0, size, size)];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
#else
NSCIImageRep *rep = [NSCIImageRep imageRepWithCIImage: ciImage];
NSImage* tinyImage = [[NSImage alloc] init];
[tinyImage addRepresentation: rep];
if (size <= rep.size.width)
return tinyImage;
// Scale image up:
NSImage* nsImage = [[NSImage alloc] initWithSize: NSMakeSize(size, size)];
[nsImage lockFocus];
[NSGraphicsContext currentContext].imageInterpolation = NSImageInterpolationNone;
[tinyImage drawInRect: NSMakeRect(0, 0, size, size)];
[nsImage unlockFocus];
return nsImage;
#endif
}
+ (CBImage*) QRCodeImageWithData: (NSData*)data {
return [self QRCodeImageWithData: data size: 500];
}
@dautermann
Copy link

thanks for this useful code! One bug/issue is at the bottom: CBImage should be QRImage.

@ingconti
Copy link

ingconti commented Jun 8, 2019

I completely rewrite it on swift. My I cite you in my public repo or we can merge these two?

@snej
Copy link
Author

snej commented Jun 18, 2019

@ingconti Sure, you can cite me; I'm not maintaining this code but you can add a link to yours here.

@xhruso00
Copy link

Outdated code. Please update with modern API UIGraphicsImageRenderer ...
UIImage *outputImage;
UIGraphicsBeginImageContextWithOptions(NSMakeSize(size, size), NO, 0);
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithBounds:CGMakeRect(0,0,size,size)];
outputImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext *ctx) {
//draw QR code
}];

@snej
Copy link
Author

snej commented Oct 28, 2019

Please update with modern API

As I said, I'm not maintaining this. But thanks for posting more recent code.

@ingconti
Copy link

ingconti commented Oct 28, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment