欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-22 20:09:16
登录注册时用的验证码效果图 viewdidload调用即可 _poocodeview = [[poocodeview alloc] initwithf...

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

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

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下载地址

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

上一篇:

下一篇: