服务器之家

服务器之家 > 正文

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

时间:2021-02-14 23:36     来源/作者:iOS开发网

先来看看效果图

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

实现方法

.h文件

?
1
2
3
4
5
6
@property (nonatomic, retain) nsarray *changearray;
@property (nonatomic, retain) nsmutablestring *changestring;
@property (nonatomic, retain) uilabel *codelabel;
 
-(void)changecode;
@end

.m文件

?
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
@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中调用

?
1
2
3
4
5
_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];

手势事件

?
1
2
3
4
- (void)tapclick:(uitapgesturerecognizer*)tap
{
  [_codeview changecode];
}

总结

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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部