Generate QR code-Swift

Source: Internet
Author: User

First import the Avfoundation framework

@IBOutlet weak var iconview:uiimageview!

Override Func Viewdidload () {

Super.viewdidload ()

Createqrcode ()

}

Private Func Createqrcode () {

Create a filter

Let Qrfilter = Cifilter (name: "Ciqrcodegenerator")

Reset the initial value of the filter

Qrfilter.setdefaults ()

To set the contents of a filter via KVC

Qrfilter.setvalue ("Deceased water peak". Datausingencoding (Nsutf8stringencoding, Allowlossyconversion:true), ForKey: " InputMessage ")

Output image

Let Ciimage = Qrfilter.outputimage

Print the size of the generated picture

println (Ciimage.extent ())

Filters for filtering image single color and deformation

Let Colorfilter = Cifilter (name: "Cifalsecolor")

Colorfilter.setdefaults ()

Colorfilter.setvalue (Ciimage, Forkey: "Inputimage")

Front view

Colorfilter.setvalue (Cicolor (red:0, green:0, blue:0), Forkey: "InputColor0")

Background color

Colorfilter.setvalue (Cicolor (red:1, Green:1, blue:1), Forkey: "InputColor1")

Let transform = Cgaffinetransformmakescale (5, 5)

Let Transformimage = ColorFilter.outputImage.imageByApplyingTransform (transform)

Let Codeimage = UIImage (ciimage:transformimage)

Let Avatarimage = UIImage (named: "Avatar")

println (Codeimage)

println ("----" + "\ (avatarimage)")

Iconview.image = UIImage (ciimage:transformimage)

Iconview.image = Insertavaterimage (codeimage!, avatarimage:avatarimage!)

}

Synthetic avatar Image

Private func Insertavaterimage (codeimage:uiimage,avatarimage:uiimage), UIImage {

Let size = Codeimage.size

1. Turn on the image context

Uigraphicsbeginimagecontext (size)

2. Drawing two-dimensional code image

Codeimage.drawinrect (CGRectMake (0, 0, size.width, size.height))

3. Calculate the size of the avatar

Let w = size.width * 0.25

Let H = size.height * 0.25

Let x = (size.width-w) * 0.5

Let y = (size.height-h) * 0.5

Avatarimage.drawinrect (CGRectMake (x, Y, W, h))

4. Remove the image from the context

Let image = Uigraphicsgetimagefromcurrentimagecontext ()

5. Close the context

Uigraphicsendimagecontext ()

return image

}

Generate QR code-Swift

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.