服务器端提供ios图形验证码,iOS 生成图片验证码绘制实例代码

登录注册时用的验证码效果图

b11e9f292d79d4945891ec103536a2df.png

viewdidload调用即可

_poocodeview = [[poocodeview alloc] initwithframe:cgrectmake(50, 100, 82, 32)];

uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(tapclick:)];

[_poocodeview addgesturerecognizer:tap];

[self.view addsubview:_poocodeview];

#import

@interface poocodeview : uiview

@property (nonatomic, retain) nsarray *changearray;

@property (nonatomic, retain) nsmutablestring *changestring;

@property (nonatomic, retain) uilabel *codelabel;

-(void)changecode;

@end

#import "poocodeview.h"

@implementation poocodeview

@synthesize changearray = _changearray;

@synthesize changestring = _changestring;

@synthesize codelabel = _codelabel;

- (id)initwithframe:(cgrect)frame

{

self = [super initwithframe:frame];

if (self) {

// initialization code

// self.layer.cornerradius = 5.0;

// self.layer.maskstobounds = yes;

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)touchesbegan:(nsset *)touches withevent:(uievent *)event

//{

// [self change];

// [self setneedsdisplay];

//}

-(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" sizewithfont:[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 withfont:[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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值