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