欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-20 23:14:58
1、搭个界面 2、写代码 // // viewcontroller.swift // generatorqrcode // // cre...

1、搭个界面

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

2、写代码

// 
// 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(二维码)实例详解

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

上一篇:

下一篇: