当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS 生成图片验证码绘制实例代码

iOS 生成图片验证码绘制实例代码

2019年07月24日  | 移动技术网移动技术  | 我要评论

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


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 <uikit/uikit.h>
@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

github下载地址

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网