Generate a QR code and a string link based on the string Link
# Pragma mark-InterpolatedUIImage = because the generated QR code is a CIImage, it is difficult to control the size if we convert it directly to a UIImage. Therefore, use the following method to return the UIImage with the required size.
-(UIImage *) createNonInterpolatedUIImageFormCIImage :( CIImage *) image withSize :( CGFloat) size {
CGRect extent = CGRectIntegral (image. extent );
CGFloat scale = MIN (size/CGRectGetWidth (extent), size/CGRectGetHeight (extent ));
// Create a bitmap image that we'll draw into a bitmap context at the desired size;
Size_t width = CGRectGetWidth (extent) * scale;
Size_t height = CGRectGetHeight (extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray ();
CGContextRef bitmapRef = CGBitmapContextCreate (nil, width, height, 8, 0, cs, (CGBitmapInfo) kCGImageAlphaNone );
CIContext * context = [CIContext contextwitexceptions: nil];
CGImageRef bitmapImage = [context createCGImage: image fromRect: extent];
CGContextSetInterpolationQuality (bitmapRef, kCGInterpolationNone );
CGContextScaleCTM (bitmapRef, scale, scale );
CGContextDrawImage (bitmapRef, extent, bitmapImage );
// Create an image with the contents of our bitmap
CGImageRef scaledImage = CGBitmapContextCreateImage (bitmapRef );
// Cleanup
CGContextRelease (bitmapRef );
CGImageRelease (bitmapImage );
Return [UIImage imageWithCGImage: scaledImage];
}
# Pragma mark-QRCodeGenerator -- first, the QR code is generated. It is very easy to use CIFilter. Just pass in the string that generates the QR code.
-(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: @ "M" forKey: @ "inputCorrectionLevel"];
// Send the image back
Return qrFilter. outputImage;
}
------------------------
Call:
UIImage * boundImg = [self createNonInterpolatedUIImageFormCIImage: [self createQRForString: @ "http://baidu.com"] withSize: 2500000f];