利用iOS绘制图片生成随机验证码示例代码

 更新时间:2016年11月13日 11:50:42   投稿:daisy  
验证码的功能一般是防止使用程序恶意注册、暴力破解或批量发帖而设置的。所谓验证码,就是将一串随机产生的数字或符号,生成一幅图片,图片里加上一些干扰象素(防止OCR),图片验证码相信大家都见到过,这篇文章用示例代码给大家介绍iOS绘制图片生成随机验证码的方法。

先来看看效果图

实现方法

.h文件

@property (nonatomic, retain) NSArray *changeArray;
@property (nonatomic, retain) NSMutableString *changeString;
@property (nonatomic, retain) UILabel *codeLabel;

-(void)changeCode;
@end

.m文件

@synthesize changeArray = _changeArray;
@synthesize changeString = _changeString;
@synthesize codeLabel = _codeLabel;

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    // Initialization code

    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.backgroundColor = color;
    [self change];
  }
  return self;
}

-(void)changeCode
{
  [self change];
  [self setNeedsDisplay];
}

- (void)change
{
  self.changeArray = [[NSArray alloc] initWithObjects:@"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",nil];

  NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:5];

  self.changeString = [[NSMutableString alloc] initWithCapacity:6];
  for(NSInteger i = 0; i < 4; i++)
  {
    NSInteger index = arc4random() % ([self.changeArray count] - 1);
    getStr = [self.changeArray objectAtIndex:index];

    self.changeString = (NSMutableString *)[self.changeString stringByAppendingString:getStr];
  }
}

- (void)drawRect:(CGRect)rect 
{
  [super drawRect:rect];

  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.5];
  [self setBackgroundColor:color];

  NSString *text = [NSString stringWithFormat:@"%@",self.changeString];
  CGSize cSize = [@"S" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
  int width = rect.size.width / text.length - cSize.width;
  int height = rect.size.height - cSize.height;
  CGPoint point;

  float pX, pY;
  for (int i = 0; i < text.length; i++)
  {
    pX = arc4random() % width + rect.size.width / text.length * i;
    pY = arc4random() % height;
    point = CGPointMake(pX, pY);
    unichar c = [text characterAtIndex:i];
    NSString *textC = [NSString stringWithFormat:@"%C", c];
    [textC drawAtPoint:point withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
  }

  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetLineWidth(context, 1.0);
  for(int cout = 0; cout < 10; cout++)
  {
    red = arc4random() % 100 / 100.0;
    green = arc4random() % 100 / 100.0;
    blue = arc4random() % 100 / 100.0;
    color = [UIColor colorWithRed:red green:green blue:blue alpha:0.2];
    CGContextSetStrokeColorWithColor(context, [color CGColor]);
    pX = arc4random() % (int)rect.size.width;
    pY = arc4random() % (int)rect.size.height;
    CGContextMoveToPoint(context, pX, pY);
    pX = arc4random() % (int)rect.size.width;
    pY = arc4random() % (int)rect.size.height;
    CGContextAddLineToPoint(context, pX, pY);
    CGContextStrokePath(context);
  }
}
@end

VIewController中调用

_codeView = [[CodeView alloc] initWithFrame:CGRectMake(15+(SCREEN_WIDTH-30)/3*2, 75, (SCREEN_WIDTH-30)/3, 39)];
 //手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
[_codeView addGestureRecognizer:tap];
[self.view addSubview: _codeView];

手势事件

- (void)tapClick:(UITapGestureRecognizer*)tap
{
  [_codeView changeCode];
}

总结

以上就是利用iOS绘制图片随机验证码的全部内容,希望本文的内容对各位iOS开发者们能有所帮助,如果有疑问大家可以留言交流。

相关文章

  • iOS开发之自定义图片拉伸功能

    iOS开发之自定义图片拉伸功能

    这篇文章主要介绍了iOS开发之自定义图片拉伸功能,需要的朋友可以参考下
    2017-06-06
  • iOS仿擦玻璃效果的实现方法

    iOS仿擦玻璃效果的实现方法

    最近在网上看到一个博客分享的这个效果很不错,就拿下来看看,结果看了好几遍也没完全看懂,再结合自己之前学的东西感觉不用这么复杂也能实现同样的效果,于是就开始动手了。现在将实现的步骤和示例代码分享给大家,有需要的朋友们可以参考借鉴。
    2016-10-10
  • IOS 开发之swift中UIView的扩展使用的实例

    IOS 开发之swift中UIView的扩展使用的实例

    这篇文章主要介绍了IOS 开发之swift中UIView的扩展使用的实例的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-09-09
  • 详解iOS自定义UITabBar与布局

    详解iOS自定义UITabBar与布局

    本篇文章给大家详细分析了iOS自定义UITabBar与布局的实际操作过程以及相关代码分享,一起学习下。
    2018-02-02
  • iOS实现全局悬浮按钮

    iOS实现全局悬浮按钮

    这篇文章主要为大家详细介绍了iOS实现全局悬浮按钮,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • ios 贝塞尔曲线切割圆角的方法

    ios 贝塞尔曲线切割圆角的方法

    本篇文章主要介绍了ios 贝塞尔曲线切割圆角的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • 关于iOS GangSDK的使用 为App快速集成社群公会模块

    关于iOS GangSDK的使用 为App快速集成社群公会模块

    这篇文章主要介绍了iOS GangSDK的使用为App快速集成社群公会模块功能的实现过程。
    2017-11-11
  • IOS给xcode工程关联pod的实例详解

    IOS给xcode工程关联pod的实例详解

    这篇文章主要介绍了IOS给xcode工程关联pod的实例详解的相关资料,希望大家通过本文能实现这样的需求,需要的朋友可以参考下
    2017-09-09
  • Flutter GetPageRoute实现嵌套导航学习

    Flutter GetPageRoute实现嵌套导航学习

    这篇文章主要为大家介绍了Flutter GetPageRoute实现嵌套导航的示例学习,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • iOS开发之枚举用法小结

    iOS开发之枚举用法小结

    大家都知道枚举是C语言中的一种基本数据类型,是一个"被命名的整型常量"的集合,它不参与内存的占用和释放,我们在开发中使用枚举的目的只有一个,那就是为了增加代码的可读性。下面就来来看看在iOS中枚举的用法,有需要的朋友们可以看看。
    2016-09-09

最新评论