小编给大家分享一下iOS如何生成图片验证码,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
专注于为中小企业提供网站建设、网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业大观免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。1.数据源
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.创建放置验证码的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.调用方法及回调
public func refreshCode(callBack: @escaping (_ str: NSString) -> ()) { initCodeView() callBack(codeStr) }6.效果图
以上是“iOS如何生成图片验证码”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联网站建设公司行业资讯频道!
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页名称:iOS如何生成图片验证码-创新互联
转载来于:http://cdweb.net/article/dihcsd.html