IOS Two-dimensional code generation

Source: Internet
Author: User

This blog will introduce the generation of two-dimensional code.

Since nothing is worth lengthy, here I introduce the QR code through the implementation of the codes.

The first part

The first part is the simple generation of two-dimensional code with no other emphasis introduced.

Code section

////viewcontroller.m//CX qr code generation////Created by Ma C on 16/4/12.//Copyright 2016 BJSXT. All rights reserved.//#import "ViewController.h" #import <CoreImage/CoreImage.h> @interface Viewcontroller () @    End@implementation viewcontroller-(void) viewdidload {[Super viewdidload]; }-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{//Create filter Cifilter * Filter =    [Cifilter filterwithname:@ "Ciqrcodegenerator"];    Restore default settings [filter SetDefaults];    Add data to the filter NSString * str = @ "Xu Bao Love to eat fish";//The text here can be URL NSData * data = [str datausingencoding:nsutf8stringencoding];    [Filter Setvalue:data forkey:@ "InputMessage"];    The output obtains the QR code ciimage * image = [filter outputimage];    Show QR Code uiimageview * ImageView = [[Uiimageview alloc]initwithframe:cgrectmake (20, 20, 100, 100)];    Imageview.image = [UIImage imagewithciimage:image];    [Self.view Addsubview:imageview]; } @end 

Although it is easy to say, it is worth noting that the native framework of the method generated by the QR code is CI so we have to deal with before the display.

Scan the first part of the paragraph is a piece of text, of course, follow the comments if you join the URL can also pop up the site.

If careful observation is not difficult to find, the two-bit code is not high definition. Let me show you how to make the sharpness of the solution higher.

Part II

The second part introduces the method of making the clarity of the two-dimensional code become higher.


Code section

viewcontroller.m//CX qr code generation////Created by Ma C on 16/4/12.//Copyright? 2016 BJSXT. All rights reserved.//#import "ViewController.h" #import <CoreImage/CoreImage.h> @interface Viewcontroller () @    End@implementation viewcontroller-(void) viewdidload {[Super viewdidload]; }-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{//Create filter Cifilter * Filter =    [Cifilter filterwithname:@ "Ciqrcodegenerator"];    Restore default settings [filter SetDefaults];    Add data to the filter NSString * str = @ "Xu Bao Love to eat fish";//The text here can be URL NSData * data = [str datausingencoding:nsutf8stringencoding];    [Filter Setvalue:data forkey:@ "InputMessage"];    The output obtains the QR code ciimage * image = [filter outputimage];    Show QR Code uiimageview * ImageView = [[Uiimageview alloc]initwithframe:cgrectmake (20, 20, 100, 100)];    Imageview.image = [self createnoninterpolateduiimageformciimage:image withsize:200]; [Self.view Addsubview:imageview];} /** * Generates uiimage of the specified size according to Ciimage * @paraM image ciimage * @param size picture width */-(UIImage *) Createnoninterpolateduiimageformciimage: (Ciimage *) image withsize: (CGF    Loat) 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 picture cgimageref Scaledimage = Cgbitmapcontextcreateimage (bitmapref);    Cgcontextrelease (BITMAPREF); CgiMagerelease (BitmapImage); return [UIImage imagewithcgimage:scaledimage];} @end

It is important to note that this method can become a category.

After these things, we have to think about a lot of times in the middle of the QR code is a picture of how to do it???

It's easy, just get yourself a picture. Overlay in the middle of the QR code (note picture size master)

IOS Two-dimensional code generation

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.