Objective-c 图片验证码
来源:互联网 发布:帝国时代2日本武士数据 编辑:程序博客网 时间:2024/05/22 07:40
- (void)onTapToGenerateCode:(UITapGestureRecognizer *)tap { MPLog(@"验证码刷新"); for (UIView *view in self.checkCodeNumberLabel.subviews) { [view removeFromSuperview]; } // @{ // @name 生成背景色 float red = arc4random() % 100 / 100.0; float green = arc4random() % 100 / 100.0; float blue = arc4random() % 100 / 100.0; UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:0.2]; [self.checkCodeNumberLabel setBackgroundColor:color]; // @} end 生成背景色 // @{ // @name 生成文字 const int count = 5; char data[count]; for (int x = 0; x < count; x++) { int j = '0' + (arc4random_uniform(75)); if((j >= 58 && j <= 64) || (j >= 91 && j <= 96)){ --x; }else{ data[x] = (char)j; } } NSString *text = [[NSString alloc] initWithBytes:data length:count encoding:NSUTF8StringEncoding]; self.code = text; // @} end 生成文字 CGSize cSize = [@"S" sizeWithFont:[UIFont systemFontOfSize:16]]; int width = self.checkCodeNumberLabel.frame.size.width / text.length - cSize.width; int height = self.checkCodeNumberLabel.frame.size.height - cSize.height;// CGPoint point; float pX, pY; for (int i = 0, count = text.length; i < count; i++) { pX = arc4random() % width + self.checkCodeNumberLabel.frame.size.width / text.length * i - 1; pY = arc4random() % height;// point = CGPointMake(pX, pY); unichar c = [text characterAtIndex:i]; UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(pX, pY, self.checkCodeNumberLabel.frame.size.width / 4, self.checkCodeNumberLabel.frame.size.height)]; tempLabel.backgroundColor = [UIColor clearColor]; // 字体颜色 float red = arc4random() % 100 / 100.0; float green = arc4random() % 100 / 100.0; float blue = arc4random() % 100 / 100.0; UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0]; NSString *textC = [NSString stringWithFormat:@"%C", c]; tempLabel.textColor = color; tempLabel.text = textC; [self.checkCodeNumberLabel addSubview:tempLabel]; } // 干扰线 CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 1.0); for(int i = 0; i < count; i++) { red = arc4random() % 100 / 100.0; green = arc4random() % 100 / 100.0; blue = arc4random() % 100 / 100.0; color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0]; CGContextSetStrokeColorWithColor(context, [color CGColor]); pX = arc4random() % (int)self.checkCodeNumberLabel.frame.size.width; pY = arc4random() % (int)self.checkCodeNumberLabel.frame.size.height; CGContextMoveToPoint(context, pX, pY); pX = arc4random() % (int)self.checkCodeNumberLabel.frame.size.width; pY = arc4random() % (int)self.checkCodeNumberLabel.frame.size.height; CGContextAddLineToPoint(context, pX, pY); CGContextStrokePath(context); } return; }
0 0
- Objective-c 图片验证码
- 生成图片验证码[C#]
- 彩色验证码图片(C#)
- Objective-C中验证Email
- Objective-C 中的CRC32验证
- objective-c 图片的操作
- objective-c处理图片缩放
- Objective-C图片切圆角优化
- 生成验证码图片(.net2005/c#)
- Objective C 验证邮箱的有效性
- objective-c post验证付款信息
- asp.net(C#)图片随机验证码的代码!
- Objective-C错误码
- MVC(C#)生成和检测图片验证码,支持多页面不同验证码
- objective-c 实现用户验证,登陆 Xcode iOS
- C#图片验证码
- 图片验证码
- 图片验证码
- android architecture
- POJ3270 Cow Sorting【置换群】【间接排序】
- 设计模式笔记-原型模式
- Java 多线程之--ThreadLocal 简介
- 关于 UML 模型(Visio的说明)
- Objective-c 图片验证码
- Using Spring to Receive JMS Messages
- 关于匹配的疑问:内积 or 二范数
- InterBase7
- 设置TOMCAT的JVM虚拟机内存大小
- 关于数组的面试题总结(一)
- Android数据库高手秘籍(零)——前言
- 基于Matlab的粒子群算法实现
- javascript,object,IDispatchEx笔记