diff --git a/QRCode/QR/UIImage+MDQRCode.h b/QRCode/QR/UIImage+MDQRCode.h index 052b4e3..485b0d7 100644 --- a/QRCode/QR/UIImage+MDQRCode.h +++ b/QRCode/QR/UIImage+MDQRCode.h @@ -33,6 +33,6 @@ */ + (UIImage *)mdQRCodeForString:(NSString *)qrString size:(CGFloat)size; + (UIImage *)mdQRCodeForString:(NSString *)qrString size:(CGFloat)size fillColor:(UIColor *)fillColor; - ++ (UIImage *)mdQRCodeForString:(NSString *)qrString size:(CGFloat)imageSize fillColor:(UIColor *)fillColor :(int *)width :(int *)version; @end diff --git a/QRCode/QR/UIImage+MDQRCode.m b/QRCode/QR/UIImage+MDQRCode.m index 01d9dcf..e77c444 100644 --- a/QRCode/QR/UIImage+MDQRCode.m +++ b/QRCode/QR/UIImage+MDQRCode.m @@ -106,4 +106,58 @@ + (UIImage *)mdQRCodeForString:(NSString *)qrString size:(CGFloat)imageSize fill return qrImage; } ++ (UIImage *)mdQRCodeForString:(NSString *)qrString size:(CGFloat)imageSize fillColor:(UIColor *)fillColor :(int *)width :(int *)version +{ + if (0 == [qrString length]) { + return nil; + } + + // generate QR + QRcode *code = QRcode_encodeString([qrString UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1); + if (!code) { + return nil; + } + + CGFloat size = imageSize * [[UIScreen mainScreen] scale]; + + if(width) + { + *width = code->width; + } + if(version) + { + *version = code->version; + } + + if (code->width > size) { + printf("Image size is less than qr code size (%d)\n", code->width); + return nil; + } + + // create context + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + + // The constants for specifying the alpha channel information are declared with the CGImageAlphaInfo type but can be passed to this parameter safely. + + CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast); + + CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size); + CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1); + CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform)); + + // draw QR on this context + [self mdDrawQRCode:code context:ctx size:size fillColor:fillColor]; + + // get image + CGImageRef qrCGImage = CGBitmapContextCreateImage(ctx); + UIImage * qrImage = [UIImage imageWithCGImage:qrCGImage]; + + // free memory + CGContextRelease(ctx); + CGImageRelease(qrCGImage); + CGColorSpaceRelease(colorSpace); + QRcode_free(code); + return qrImage; +} + @end diff --git a/QRCodeEncoder.podspec b/QRCodeEncoder.podspec new file mode 100644 index 0000000..76ca0d4 --- /dev/null +++ b/QRCodeEncoder.podspec @@ -0,0 +1,15 @@ +Pod::Spec.new do |s| + s.name = 'QRCodeEncoder' + s.version = '1.0' + s.platform = :ios, '7.0' + s.license = { :type => 'MIT', :file => 'LICENSE' } + s.summary = 'QRCode Encoder For Your iOS app.' + s.homepage = 'https://github.com/Notan/ios-qr-code-encoder' + s.authors = { 'Alex Zarochintsev' => 'alex.zarochintsev@icloud.com' } + s.source = { :git => 'https://github.com/Notan/ios-qr-code-encoder', :tag => "1.0" } + + s.source_files = 'QRCode/QR/**/*.{h,m,c}' + + s.framework = 'Foundation', 'UIKit' + s.requires_arc = true +end \ No newline at end of file