IOS Swift 開發(fā)QRCore(二維碼)實(shí)例詳解
1、搭個(gè)界面
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)//處理成為一個(gè)高清圖片 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、運(yùn)行效果
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
iOS開發(fā)中Date Picker和UITool Bar控件的使用簡(jiǎn)介
這篇文章主要介紹了iOS開發(fā)中Date Picker和UITool Bar控件的使用簡(jiǎn)介,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01iOS學(xué)習(xí)教程之UIView中坐標(biāo)轉(zhuǎn)換詳解
這篇文章主要給大家介紹了關(guān)于iOS UIView中坐標(biāo)轉(zhuǎn)換的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)跟著小編一起看看吧。2017-05-05iOS登錄時(shí)驗(yàn)證手機(jī)號(hào)與倒計(jì)時(shí)發(fā)送驗(yàn)證碼問(wèn)題詳解
這篇文章主要給大家介紹了關(guān)于iOS登錄時(shí)驗(yàn)證手機(jī)號(hào)與倒計(jì)時(shí)發(fā)送驗(yàn)證碼問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2019-01-01淺談iOS關(guān)于頭文件的導(dǎo)入問(wèn)題
本篇文章主要介紹了淺談iOS關(guān)于頭文件的導(dǎo)入問(wèn)題,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04iOS UICollectionView刷新時(shí)閃屏的解決方法
本篇文章主要介紹了iOS UICollectionView刷新時(shí)閃屏的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11iOS開發(fā)技巧之WeakSelf宏的進(jìn)化詳解
在程序中我們經(jīng)常用到Block,但寫weak self 時(shí)會(huì)比較繁瑣,下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)技巧之WeakSelf宏的進(jìn)化的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們一起來(lái)看看吧2018-05-05簡(jiǎn)單說(shuō)說(shuō)iOS之WKWebView的用法小結(jié)
iOS8.0之后我們使用 WebKit框架中的WKWebView來(lái)加載網(wǎng)頁(yè)。這篇文章主要介紹了簡(jiǎn)單說(shuō)說(shuō)iOS之WKWebView的用法小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01