Birth to a QR code based on a string chain

Source: Internet
Author: User

#pragma mark-interpolateduiimage= because the generated QR code is a ciimage, we switch directly to UIImage word size is not good control, so use the following method to return the size of the UIImage

-(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 contextwithoptions: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 is the generation of two-dimensional code, using cifilter is very simple, directly into the generation of two-dimensional code of the string can be

-(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:250.0f];

Birth to a QR code based on a string chain

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.