IOS Swift 开发QRCore(二维码)实例详解

时间:2022-09-20 09:43:57

1、搭个界面

IOS Swift 开发QRCore(二维码)实例详解

2、写代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// viewcontroller.swift
// generatorqrcode
//
// created by targetcloud on 2016/12/3.
// copyright © 2016年 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)//处理成为一个高清图片
    image = image?.applying(transform)
    var resultimage = uiimage(ciimage: image!)
     
    let centerimg = uiimage(named: "targetcloud.png")
    resultimage = mergeimage(resultimage, centerimg: centerimg!, drawsize:cgsize(width: 80, height: 80))
    qrimg.image = resultimage
  }
   
  func mergeimage(_ sourceimage: uiimage, centerimg: uiimage,drawsize:cgsize) -> uiimage {
    let size = sourceimage.size
    uigraphicsbeginimagecontext(size)
    sourceimage.draw(in: cgrect(x: 0, y: 0, width: size.width, height: size.height))
    centerimg.draw(in: cgrect(x: (size.width - drawsize.width) * 0.5, y: (size.height - drawsize.height) * 0.5, width: drawsize.width, height: drawsize.height))
    let resultimage = uigraphicsgetimagefromcurrentimagecontext()
    uigraphicsendimagecontext()
    return resultimage!
  }
}

3、运行效果

IOS Swift 开发QRCore(二维码)实例详解

IOS Swift 开发QRCore(二维码)实例详解

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/callzjy/article/details/53451699