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

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

程序员文章站 2023-12-22 11:26:10
先来看看效果图 实现方法 .h文件 @property (nonatomic, retain) nsarray *changearray; @pro...

先来看看效果图

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

实现方法

.h文件

@property (nonatomic, retain) nsarray *changearray;
@property (nonatomic, retain) nsmutablestring *changestring;
@property (nonatomic, retain) uilabel *codelabel;

-(void)changecode;
@end

.m文件

@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中调用

_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];

手势事件

- (void)tapclick:(uitapgesturerecognizer*)tap
{
  [_codeview changecode];
}

总结

以上就是利用ios绘制图片随机验证码的全部内容,希望本文的内容对各位ios开发者们能有所帮助,如果有疑问大家可以留言交流。

上一篇:

下一篇: