Generate a QR code and a string link based on the string Link

Source: Internet
Author: User

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];

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.