Skip to content

Instantly share code, notes, and snippets.

@xslim
Created July 22, 2014 07:41
Show Gist options
  • Save xslim/3561cf7f9aae9a5bd43b to your computer and use it in GitHub Desktop.
Save xslim/3561cf7f9aae9a5bd43b to your computer and use it in GitHub Desktop.
UIImage category for generating QR code
#import "UIImage+QRCode.h"
#import <CoreImage/CoreImage.h>
@implementation UIImage (QRCode)
+ (instancetype)imageWithQRCode:(NSString *)code
{
if (code.length == 0) return nil;
NSData *data = [code dataUsingEncoding:NSUTF8StringEncoding];
if (data.length == 0) return nil;
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults];
[filter setValue:data forKey:@"inputMessage"];
[filter setValue:@"H" forKey:@"inputCorrectionLevel"];
CIImage *outputImage = [filter outputImage];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:outputImage
fromRect:[outputImage extent]];
UIImage *image = [UIImage imageWithCGImage:cgImage
scale:1.
orientation:UIImageOrientationUp];
// Resize without interpolating
UIImage *resized = [self _resizeImage:image
withQuality:kCGInterpolationNone
rate:6.0];
CGImageRelease(cgImage);
return resized;
}
+ (UIImage *)_resizeImage:(UIImage *)image
withQuality:(CGInterpolationQuality)quality
rate:(CGFloat)rate
{
UIImage *resized = nil;
CGFloat width = image.size.width * rate;
CGFloat height = image.size.height * rate;
UIGraphicsBeginImageContext(CGSizeMake(width, height));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, quality);
[image drawInRect:CGRectMake(0, 0, width, height)];
resized = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resized;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment