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

2022-10-21,,,

本篇文章主要介绍了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下载地址

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

您可能感兴趣的文章:

  • iOS开发实现随机图片验证码封装
  • iOS 生成图片验证码(实用功能)
  • 利用iOS绘制图片生成随机验证码示例代码
  • IOS实现验证码倒计时功能(一)
  • iOS获取短信验证码倒计时的两种实现方法
  • iOS本地动态生成验证码的方法
  • IOS实现验证码倒计时功能(二)
  • iOS生成图片数字字母验证效果

《iOS 生成图片验证码绘制实例代码.doc》

下载本文的Word格式文档,以方便收藏与打印。