Created
September 21, 2015 16:48
-
-
Save snej/c210cc4cbfe8fd277186 to your computer and use it in GitHub Desktop.
Shows how to generate a QRCode on iOS or Mac OS X
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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]; | |
} |
ingconti
commented
Oct 28, 2019
via email
You are welcome
On 28 Oct 2019, at 18:05, Jens Alfke ***@***.***> wrote:
Please update with modern API
As I said, I'm not maintaining this. But thanks for posting more recent code.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <https://gist.github.com/c210cc4cbfe8fd277186?email_source=notifications&email_token=AA64MJ6RM3DOZDRKKXOY5QTQQ4LULA5CNFSM4HWG7FIKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF3IIS#gistcomment-3068041>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AA64MJ7ABKX3CHQTF265RX3QQ4LULANCNFSM4HWG7FIA>.
ing. Conti G. Enrico
[email protected]
Ordine Ingegneri di Monza al n. A894
PIVA 02119300966
via Duca degli Abruzzi 29
20090 MONZA MI ITALY
0039-039-324723
www.ingconti.com
CODICE DESTINATARIO
KRRH6B9
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment