IOS Swift develops Qrcore (two-dimensional code) examples detailed _ios

Source: Internet
Author: User

1. Build an interface


2, write code

Viewcontroller.swift//Generatorqrcode////Created by Targetcloud on 2016/12/3. COPYRIGHT©2016 year Targetcloud. 
All rights reserved. 
  Import Uikit class Viewcontroller:uiviewcontroller {@IBOutlet weak var qrimg:uiimageview! 
 
  @IBOutlet weak var qrcontent:uitextview! @IBAction Func generatorqrimg (_ Sender:any) {view.endediting (true) let str = Qrcontent.text?? "Let filter = Cifilter (name:" Ciqrcodegenerator ") filter? SetDefaults () Let data = Str.data (Using:String.Encoding.utf8) filter? SetValue (data, Forkey: "InputMessage") filter? SetValue ("M", Forkey: "Inputcorrectionlevel") var image = filter? Outputimage Let transform = Cgaffinetransform (scalex:10, y:10)//processing becomes an HD image image = Image? 
     
    Applying (transform) var resultimage = UIImage (ciimage:image!) Let centerimg = UIImage (named: "Targetcloud.png") Resultimage = Mergeimage (Resultimage, centerimg:centerimg!, DrawSi Ze:cGsize (width:80, height:80)) Qrimg.image = Resultimage} func mergeimage (_ Sourceimage:uiimage, centerimg : uiimage,drawsize:cgsize)-> uiimage {Let size = sourceimage.size uigraphicsbeginimagecontext (size) so Urceimage.draw (In:cgrect (x:0, y:0, Width:size.width, Height:size.height)) Centerimg.draw (In:cgrect (x: Size.wid 
    Th-drawsize.width) * 0.5, Y: (size.height-drawsize.height) * 0.5, Width:drawSize.width, height:drawSize.height)) 
  Let Resultimage = Uigraphicsgetimagefromcurrentimagecontext () uigraphicsendimagecontext () return resultImage!  } 
}

3. Operation effect



Thank you for reading, I hope to help you, thank you for your support for this site!

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.