iOS生成图片数字字母验证效果

时间:2022-11-17 22:41:12

本文实例为大家分享了ios生成图片数字字母验证的具体代码,供大家参考,具体内容如下

直接上代码,注释很详细

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#import "captchaview.h"
 
#define krandomcolor [uicolor colorwithred:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0];
 
//#define krandomcolor [uicolor graycolor];
#define klinecount 6
#define klinewidth 1.0
#define kcharcount 4
#define kfontsize [uifont systemfontofsize:arc4random() % 5 + 15]
 
@implementation captchaview
@synthesize changestring,changearray;
 
- (instancetype)initwithframe:(cgrect)frame
{
  if (self = [super initwithframe:frame]) {
 
    self.layer.cornerradius = 5.0; //设置layer圆角半径
    self.layer.maskstobounds = yes; //隐藏边界
    self.backgroundcolor = krandomcolor;
 
    //    [uicolor graycolor]
 
    //显示一个随机验证码
    [self changecaptcha];
  }
 
  return self;
}
#pragma mark 更换验证码,得到更换的验证码的字符串
-(void)changecaptcha
{
  //<一>从字符数组中随机抽取相应数量的字符,组成验证码字符串
  //数组中存放的是全部可选的字符,可以是字母,也可以是中文
  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];
 
  //如果能确定最大需要的容量,使用initwithcapacity:来设置,好处是当元素个数不超过容量时,添加元素不需要重新分配内存
  nsmutablestring *getstr = [[nsmutablestring alloc] initwithcapacity:kcharcount];
  self.changestring = [[nsmutablestring alloc] initwithcapacity:kcharcount];
 
  //随机从数组中选取需要个数的字符,然后拼接为一个字符串
  for(int i = 0; i < kcharcount; i++)
  {
    nsinteger index = arc4random() % ([self.changearray count] - 1);
    getstr = [self.changearray objectatindex:index];
 
    self.changestring = (nsmutablestring *)[self.changestring stringbyappendingstring:getstr];
  }
}
 
#pragma mark 点击view时调用,因为当前类自身就是uiview,点击更换验证码可以直接写到这个方法中,不用再额外添加手势
-(void)touchesbegan:(nsset *)touches withevent:(uievent *)event
{
  //点击界面,切换验证码
  [self changecaptcha];
 
  //setneedsdisplay调用drawrect方法来实现view的绘制
  [self setneedsdisplay];
}
 
#pragma mark 绘制界面(1.uiview初始化后自动调用; 2.调用setneedsdisplay方法时会自动调用)
- (void)drawrect:(cgrect)rect {
  // 重写父类方法,首先要调用父类的方法
  [super drawrect:rect];
 
  //设置随机背景颜色
  self.backgroundcolor = krandomcolor;
 
  //获得要显示验证码字符串,根据长度,计算每个字符显示的大概位置
  nsstring *text = [nsstring stringwithformat:@"%@",self.changestring];
  cgsize csize = [@"s" sizewithattributes:@{nsfontattributename:[uifont systemfontofsize:20.0]}];
  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:kfontsize}];
  }
 
    //调用drawrect:之前,系统会向栈中压入一个cgcontextref,调用uigraphicsgetcurrentcontext()会取栈顶的cgcontextref
    cgcontextref context = uigraphicsgetcurrentcontext();
    //设置画线宽度
    cgcontextsetlinewidth(context, klinewidth);
 
    //绘制干扰的彩色直线
    for(int i = 0; i < klinecount; i++)
    {
      //设置线的随机颜色
      uicolor *color = krandomcolor;
      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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/bobbob32/article/details/77527553