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

时间:2022-09-19 21:08:56

先来看看效果图

利用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开发者们能有所帮助,如果有疑问大家可以留言交流。