iOS development tips to quickly generate two-dimensional code

Source: Internet
Author: User

With the development of mobile Internet, two-dimensional code application is very common, the major shopping malls, restaurants, fruit shops have a two-dimensional code figure, then iOS how to generate two-dimensional code it? The following program shows a quick way to generate two-dimensional code:

To create a QR code in iOS, you need a framework: #import <CoreImage/CoreImage.h> All first we need to introduce this framework into our project.

Here's the core code:

Preparation: First add a uiimageview inside the storyboard and then take off the line to the controller inside the name of the ImageView and then controller viewdidload inside write the following code can generate two-dimensional code:

//Creating FiltersCifilter *filter = [Cifilter filterwithname:@"Ciqrcodegenerator"]; //Restore default Settings[Filter setdefaults]; //Add DataNSString *str =@"http://www.xianhua.cn"; NSData*data =[str datausingencoding:nsutf8stringencoding]; [Filter setvalue:data Forkeypath:@"InputMessage"]; //Output PictureCiimage *image =[Filter outputimage]; //Show Pictures   self.imageView.image = [UIImage imagewithciimage:image];

The results are as follows:

With the simple code above you can generate a QR code, but careful you may find that we generated by the code above the QR code is very vague, to the user experience is not very good, this time we need to deal with the image

Here is a way to use a bitmap to deal with the picture if you do not understand it's okay just put it into your project, the code is as follows:

/** * ciimage generate a specified size UIImage * * @param image Ciimage * @param size picture width*/-(UIImage *) Createnoninterpolateduiimageformciimage: (Ciimage *) Image withsize: (cgfloat) size{cgrect extent=cgrectintegral (image.extent); CGFloat Scale= MIN (Size/cgrectgetwidth (extent), size/Cgrectgetheight (extent)); //1. Create bitmap;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); //2. Save Bitmap to PictureCgimageref Scaledimage =cgbitmapcontextcreateimage (BITMAPREF);    Cgcontextrelease (BITMAPREF);        Cgimagerelease (BitmapImage); return[UIImage imagewithcgimage:scaledimage];}

With this approach, the next thing we need to do is to make a little change to the code we just started, and the Gaocheng is modified as follows:

A line of code was modified, and then a method of processing the picture was introduced, allowing the effect to be as follows:

See, is it more clear than the beginning, huh? About the generation of two-dimensional code here's the next section we introduce the QR code scanning

iOS development tips to quickly generate two-dimensional code

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.