验证码之图像验证码
来源:互联网 发布:sqlyog导入数据库 编辑:程序博客网 时间:2024/05/18 02:18
1、在App的开发中,各种验证码层出不穷。
现在主流的验证码方式是: 1、手机验证码:就是用户使用手机获取短信得到验证码,在输入App中。2、就是App 生成图形,展示在用户的手机上,用户根据图片展示输入验证码。下面我们就对第二种进行探究。
2、先看本博客实现的效果
3、呼朋唤友
群的特点:
1、iOS 开发交流群,每周都有更新新的内容。
2、群里有3~5年的资深开发者。
3、群员在App开发过程中遇到什么问题,可以在群里提问。
4、群员在App开发中,如果遇到难实现的功能或者模块,可以在群里提出,有人员帮助你实现。
5、加入群后,可以get到App 开发中的一些小功能模块。
群号是:185341804 群名字:成功QQ吧
群主号:1542100658 (qq)
4、代码展示
1》 工程中的使用代码
VerificationCodeLable * lable = [[VerificationCodeLablealloc]initWithFrame:CGRectMake(130,100+ i*80,100, 30)];
[self.viewaddSubview:lable];
/**
获取验证码的值
*/
-(void)getVerificationCodeValue{
/**
防止重复点击
*/
CFAbsoluteTime startTime =CFAbsoluteTimeGetCurrent();
CFAbsoluteTime saveTime = [[[NSUserDefaultsstandardUserDefaults] objectForKey:@"CurrentTime"]doubleValue];
if (startTime -saveTime >2000) {
[[NSUserDefaultsstandardUserDefaults] setObject:@(startTime)forKey:@"CurrentTime"];
[[NSUserDefaultsstandardUserDefaults] synchronize];
/**
创建接受数据
*/
responseData = [NSMutableDatadataWithCapacity:0];
/**
可以重新获取验证码的值
*/
NSURLSession * session = [NSURLSessionsessionWithConfiguration:[NSURLSessionConfigurationdefaultSessionConfiguration]delegate:selfdelegateQueue:[NSOperationQueuemainQueue]];
NSURLSessionDataTask * Task = [sessiondataTaskWithURL:[NSURLURLWithString:@""]];
[Task resume];
}
}
/**
颜色随机获取
@return 颜色对象
*/
-(UIColor*)colorAcquisition:(float)alpha{
CGFloat R,G,B =0.0;
R = arc4random() %255 /255.0;
G = arc4random() %255 /255.0;
B = arc4random() %255 /255.0;
UIColor * color = [UIColorcolorWithRed:R green:Gblue:B alpha:alpha];
return color;
}
/**
绘制背景线条
*/
-(void)drawBackLine:(CGRect)rect{
/**
获取上下文
*/
CGContextRef contextRef =UIGraphicsGetCurrentContext();
/**
色值画笔的宽度
*/
CGContextSetLineWidth(contextRef,1.0f);
/**
获取绘制点
*/
for (unsigned i =0 ; i<8; i++) {
/**
获取画笔的颜色
*/
CGContextSetStrokeColorWithColor(contextRef, [selfcolorAcquisition:0.25f].CGColor);
/**
获取绘制的初始点
*/
float pX =arc4random() % (int)rect.size.width;
float pY =arc4random() % (int)rect.size.height;
/**
移动画笔
*/
CGContextMoveToPoint(contextRef, pX, pY);
float pend_X =arc4random() % (int)rect.size.width;
float pend_Y =arc4random() % (int)rect.size.height;
CGContextAddLineToPoint(contextRef, pend_X, pend_Y);
CGContextStrokePath(contextRef);
}
}
- (void)drawRect:(CGRect)rect {
[superdrawRect:rect];
/**
计算单个字体的大小
*/
CGSize singleSize = [@"Z"sizeWithAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:15]}];
/**
单个字体剩余的宽
*/
CGFloat singleSurplusWeight = rect.size.width /verificationCodeString.length - singleSize.width;
/**
单个字体剩余的宽
*/
CGFloat singleSurplusHeight = rect.size.height - singleSize.height;
/**
获取绘制参数,并开始绘制
*/
float pX ,pY;
for (unsigned i =0; i<verificationCodeString.length; i++) {
/**
随机获取绘制的点
*/
pX = arc4random() % (int)singleSurplusWeight + rect.size.width /verificationCodeString.length* i;
pY = arc4random() % (int)singleSurplusHeight;
/**
获取要绘制的字
*/
unichar objectChar = [verificationCodeStringcharacterAtIndex:i];
NSString * drawStr = [NSString stringWithFormat:@"%c",objectChar];
/**
绘制对象
*/
[drawStr drawAtPoint:CGPointMake(pX, pY)withAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:15],NSForegroundColorAttributeName:[selfcolorAcquisition:1.0]}];
}
/**
创建文本底线
*/
[selfdrawBackLine:rect];
}
5、代码下载
- 验证码之图像验证码
- PHP之创建图像验证码
- PHP之创建图像验证码 ---实例
- PHP之创建图像验证码 ---实例
- [php] 图像之简单验证码制作
- 产生验证码图像
- 生成验证码图像
- 图像验证码识别
- 如何绘制图像验证码
- 验证码.图像的生成
- jsp-servlet图像验证码
- JSP生成验证码图像
- PHP生成图像验证码
- PHP图形操作之生成图像验证码
- PHP验证码之数字验证码
- PHP验证码之字符验证
- php验证码之汉字验证
- php验证码之图片验证
- nyoj 20 吝啬的国度
- request.setAttribute的用法
- morphia API汇总
- poj2109_(数学公式)
- RxJava—入门介绍
- 验证码之图像验证码
- Install gcc-5.3.0 on CentOS 7
- PHP: Wordpress 之删除 RSS 功能 的"文章RSS"、"评论RSS"、"WordPress.org"
- 数据库事务的四大特性以及事务的隔离级别
- 生产制造企业生产数据管理的四大原因
- Linux进程描述符task_struct结构体详解
- Zookeeper
- T2开发板无法连接WIFI
- VS2008远程调试