时间:2021-05-02
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.随机生成验证码
? 1 2 3 4 5 6 7 8 9 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
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 for i in 0..<codeStr.length { let pxx = arc4random() % UInt32(randWidth) let px = CGFloat(pxx) + CGFloat(i)*(self.frame.size.width-3)/CGFloat(codeStr.length) let py = arc4random() % UInt32(randHeight) let label = UILabel.init(frame: CGRect.init(x: px + 3, y: CGFloat(py), width: textSize.width, height: textSize.height)) label.text = String.init(format: "%C", codeStr.character(at: i)) label.font = UIFont.systemFont(ofSize: 20) if (isRotation == true) { let a = arc4random() % UInt32(60) let r = Double(a)-30 label.transform = CGAffineTransform.init(rotationAngle: CGFloat(r*M_PI)/180) } bgView.addSubview(label) }4.底部view是随便画几条细线
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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.调用方法及回调
? 1 2 3 4 public func refreshCode(callBack: @escaping (_ str: NSString) -> ()) { initCodeView() callBack(codeStr) }6.效果图
7.源码地址
以上所述是小编给大家介绍的iOS 生成图片验证码(实用功能),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/three_zhang/article/details/60577916
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
这是一个用PHP生成GIF动画来实现动态图片验证码的程序,非常实用,是php用户注册登录界面经常需要用到的验证码程序,有需要GIF动态图片验证码功能的朋友可以收
php生成验证码的函数,实用靠谱。先上下生成的验证码的效果图(这里生成的是全数字的验证码的示例效果):下面是php生成验证码的源码:
本文以实例演示5种验证码,并介绍生成验证码的函数。PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session中。session_
本文以实例演示5种验证码,并介绍生成验证码的函数。PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session中。1、HTML5中
本文介绍了SpringBoot结合SpringSecurity实现图形验证码功能,分享给大家,具体如下:生成图形验证码根据随机数生成图片将随机数存到Sessio