IOS Swift 开发QRCore(二维码)实例详解
更新时间:2016年12月04日 11:56:31 投稿:lqh
这篇文章主要介绍了IOS Swift 开发QRCore(二维码)实例详解的相关资料,这里对开发二维码进行了详细介绍,需要的朋友可以参考下
1、搭个界面
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、运行效果
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:
相关文章
Objective-C的UIStackView常用属性函数学习笔记
这篇文章主要为大家介绍了Objective-C的UIStackView常用属性函数学习笔记,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-03-03
最新评论