gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章利用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开发者们能有所帮助,如果有疑问大家可以留言交流.

最后此篇关于利用iOS绘制图片生成随机验证码示例代码的文章就讲到这里了,如果你想了解更多关于利用iOS绘制图片生成随机验证码示例代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com