Editado Hay preguntas similares de esta publicación, he vinculado a esta respuesta.
Utilicé este código simple para codificar un código QR en uno de mis proyectos. Se puede utilizar:
//
// QRCodeGeneratorViewController.h
//
//
// Created by Jhoney Lopes on 28/09/14.
// Copyright (c) 2014 Jhoney Lopes. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface QRCodeGeneratorViewController : UIViewController
@end
.m Implementación
//
// QRCodeGeneratorViewController.m
//
//
// Created by Jhoney Lopes on 28/09/14.
// Copyright (c) 2014 Jhoney Lopes. All rights reserved.
//
#import "QRCodeGeneratorViewController.h"
@interface QRCodeGeneratorViewController()
@property (strong, nonatomic) IBOutlet UIImageView *qrImageView;
@end
@implementation QRCodeGeneratorViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self generateTheQRCodeImageFromDataBaseInfo:@"TEXT-WHAT-YOU-WANT-TO-CONVERT-IN-QRCODE"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - QR Code Generator
- (CIImage *)createQRForString:(NSString *)qrString
{
// Need to convert the string to a UTF-8 encoded NSData object
NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding];
// Create the filter
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// Set the message content and error-correction level
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];
// Send the image back
return qrFilter.outputImage;
}
- (UIImage *)createNonInterpolatedUIImageFromCIImage:(CIImage *)image withScale:(CGFloat)scale
{
// Render the CIImage into a CGImage
CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:image fromRect:image.extent];
// Now we'll rescale using CoreGraphics
UIGraphicsBeginImageContext(CGSizeMake(image.extent.size.width * scale, image.extent.size.width * scale));
CGContextRef context = UIGraphicsGetCurrentContext();
// We don't want to interpolate (since we've got a pixel-correct image)
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
// Get the image out
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// Tidy up
UIGraphicsEndImageContext();
CGImageRelease(cgImage);
return scaledImage;
}
- (void)generateTheQRCodeImageFromDataBaseInfo:(NSString *)jsonString {
// Get the string
NSString *stringToEncode = jsonString;
// Generate the image
CIImage *qrCode = [self createQRForString:stringToEncode];
// Convert to an UIImage
UIImage *qrCodeImg = [self createNonInterpolatedUIImageFromCIImage:qrCode withScale:2*[[UIScreen mainScreen] scale]];
// And push the image on to the screen
self.qrImageView.image = qrCodeImg;
}
que necesito para generar la imagen sin conexión. –
Se editó la respuesta para incluir el complemento Phonegap que permite la codificación de códigos qr mediante programación. –