Automatic generation of two-dimensional code with small heads in swift

Source: Internet
Author: User

MARK:-Pass in a string and create a QR code image

Private Func Creatqrcodeimage (text:string, sizeh:cgfloat), uiimage{

Text is the QR code to bring in the information, Sizeh is the height of the two-dimensional code

Creating filters

Let filter = Cifilter (name: "Ciqrcodegenerator")!

To restore the default properties of a filter

Filter.setdefaults ()

Set up data that requires the generation of a two-dimensional code

Filter.setvalue (Text.datausingencoding (nsutf8stringencoding), Forkey: "InputMessage")

Remove the resulting picture from the filter

Let Ciimage = Filter.outputimage

This is a bad definition.

Let Bgimage = UIImage (ciimage:ciimage!)

This is a good definition.

Let Bgimage = Createnoninterpolateduiimageformciimage (ciimage!, Size:sizeh)

Create an Avatar

Let icon = UIImage (named: "Icon")

Composite picture (merge QR code and avatar)

Let NewImage = Creatimage (Bgimage, iconimage:icon!)

Returns the generated QR code.

Return NewImage

}

MARK:-According to the background image and avatar to synthesize the avatar QR Code, that is, the middle with the picture of the two-dimensional code

Private Func Creatimage (Bgimage:uiimage, iconimage:uiimage), uiimage{

Open Picture Context

Uigraphicsbeginimagecontext (Bgimage.size)

Draw a background picture

Bgimage.drawinrect (CGRect (Origin:cgpointzero, size:bgImage.size))

Drawing an Avatar

Let Width:cgfloat = 50

Let Height:cgfloat = width

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

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

Iconimage.drawinrect (CGRect (x:x, Y:y, Width:width, Height:height))

Take out the drawn picture

Let NewImage = Uigraphicsgetimagefromcurrentimagecontext ()

Close context

Uigraphicsendimagecontext ()

Return to the synthesized picture

Return NewImage

}

MARK:-Generates a specified size of HD uiimage based on Ciimage

Private Func Createnoninterpolateduiimageformciimage (Image:ciimage, size:cgfloat), UIImage {

Let Extent:cgrect = Cgrectintegral (image.extent)

Let scale:cgfloat = min (Size/cgrectgetwidth (extent), size/cgrectgetheight (extent))

Let width = cgrectgetwidth (extent) * scale

Let height = cgrectgetheight (extent) * scale

Let Cs:cgcolorspaceref = Cgcolorspacecreatedevicegray ()!

Let Bitmapref = cgbitmapcontextcreate (nil, int (width), int (height), 8, 0, CS, 0)!

Let context = Cicontext (Options:nil)

Let Bitmapimage:cgimageref = context.createcgimage (image, Fromrect:extent)

Cgcontextsetinterpolationquality (Bitmapref, Cginterpolationquality.none)

CGCONTEXTSCALECTM (bitmapref, scale, scale);

Cgcontextdrawimage (Bitmapref, extent, bitmapImage);

Let Scaledimage:cgimageref = Cgbitmapcontextcreateimage (bitmapref)!

Return UIImage (Cgimage:scaledimage)

}

Automatic generation of two-dimensional code with small heads in 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.