小編給大家分享一下iOS如何生成圖片驗證碼,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供烏魯木齊網(wǎng)站建設(shè)、烏魯木齊做網(wǎng)站、烏魯木齊網(wǎng)站設(shè)計、烏魯木齊網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、烏魯木齊企業(yè)網(wǎng)站模板建站服務(wù),十多年烏魯木齊做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
1.數(shù)據(jù)源
codeArray = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
2.隨機生成驗證碼
for index in 0..<4 { let i = (Int)(arc4random()) % (codeArray.count-1) let str = codeArray[i] if index == 0 { codeStr = str as NSString }else { codeStr = "\(codeStr)\(str)" as NSString } }
3.創(chuàng)建放置驗證碼的label
for i in 0..4.底部view是隨便畫幾條細線
for _ in 0...9 { let path = UIBezierPath.init() let pX = arc4random() % UInt32(self.frame.width) let pY = arc4random() % UInt32(self.frame.height) path.move(to: CGPoint.init(x: CGFloat(pX), y: CGFloat(pY))) let ptX = arc4random() % UInt32(self.frame.width) let ptY = arc4random() % UInt32(self.frame.height) path.addLine(to: CGPoint.init(x: CGFloat(ptX), y: CGFloat(ptY))) let layer = CAShapeLayer.init() layer.strokeColor = UIColor.getRandomColor(withAlpha: 0.2).cgColor//layer的邊框色 layer.lineWidth = 1.0 layer.strokeEnd = 1 layer.fillColor = UIColor.clear.cgColor layer.path = path.cgPath bgView.layer.addSublayer(layer) }5.調(diào)用方法及回調(diào)
public func refreshCode(callBack: @escaping (_ str: NSString) -> ()) { initCodeView() callBack(codeStr) }6.效果圖
以上是“iOS如何生成圖片驗證碼”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當前題目:iOS如何生成圖片驗證碼
本文來源:http://weahome.cn/article/jhdiih.html