iOS development Swift uses Cifilter to generate a two-dimensional code picture (QRCode)

Source: Internet
Author: User

Cifilter provides a wide variety of filters, where ciqrcodegenerator can be used to generate two-dimensional code. The following example shows how to generate a two-dimensional code picture of a string (a link is also a string), while also supporting the placement of personalized icons in the middle of a two-dimensional code.

The effect chart is as follows:

The code is as follows Copy Code
Import Uikit

Class Viewcontroller:uiviewcontroller {

Override Func Viewdidload () {
Super.viewdidload ()

Two-dimensional code picture with no picture
Let ImageViewIcon1 = Uiimageview (Frame:cgrectmake (20, 20, 150, 150))
Imageviewicon1.image = createqrforstring ("Http://www.111cn.net",
Qrimagename: "")
Self.view.addSubview (ImageViewIcon1)

Two-dimensional code picture with pictures
Let ImageViewIcon2 = Uiimageview (Frame:cgrectmake (200, 20, 150, 150))
Imageviewicon2.image = createqrforstring ("Http://www.111cn.net",
Qrimagename: "logo")
Self.view.addSubview (ImageViewIcon2)
}

Create a two-dimensional code picture
Func createqrforstring (qrstring:string, qrimagename:string?)-> UIImage? {
If let sureqrstring = qrstring {
Let StringData = sureqrstring.datausingencoding (nsutf8stringencoding,
Allowlossyconversion:false)
Create a two-dimensional code filter
Let Qrfilter = Cifilter (name: "Ciqrcodegenerator")!
Qrfilter.setvalue (StringData, Forkey: "InputMessage")
Qrfilter.setvalue ("H", Forkey: "Inputcorrectionlevel")
Let Qrciimage = Qrfilter.outputimage
Create a color filter, black and white
Let Colorfilter = Cifilter (name: "Cifalsecolor")!
Colorfilter.setdefaults ()
Colorfilter.setvalue (Qrciimage, Forkey: "Inputimage")
Colorfilter.setvalue (Cicolor (red:0, green:0, blue:0), Forkey: "InputColor0")
Colorfilter.setvalue (Cicolor (red:1, Green:1, blue:1), Forkey: "InputColor1")
Returns two-dimensional code image
Let Codeimage = UIImage (ciimage:colorfilter.outputimage!
. Imagebyapplyingtransform (Cgaffinetransformmakescale (5, 5))
Normally, two-dimensional codes are custom-coded, with images in the middle that want to express meaning.
If Let Iconimage = UIImage (named:qrimagename!) {
Let rect = CGRectMake (0, 0, codeImage.size.width, codeImage.size.height)
Uigraphicsbeginimagecontext (Rect.size)

Codeimage.drawinrect (Rect)
Let avatarsize = Cgsizemake (Rect.size.width * 0.25, Rect.size.height * 0.25)
Let x = (rect.width-avatarsize.width) * 0.5
Let y = (rect.height-avatarsize.height) * 0.5
Iconimage.drawinrect (CGRectMake (x, Y, Avatarsize.width, avatarsize.height))
Let Resultimage = Uigraphicsgetimagefromcurrentimagecontext ()

Uigraphicsendimagecontext ()
Return Resultimage
}
Return Codeimage
}
return Nil
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}
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.