IOS 生成圖片數字字母驗證

NO IMAGE

直接上程式碼,註釋很詳細

#import "CaptchaView.h"
#define kRandomColor  [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0];
//#define kRandomColor  [UIColor grayColor];
#define kLineCount 6
#define kLineWidth 1.0
#define kCharCount 4
#define kFontSize [UIFont systemFontOfSize:arc4random() % 5   15]
@implementation CaptchaView
@synthesize changeString,changeArray;
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.layer.cornerRadius = 5.0; //設定layer圓角半徑
self.layer.masksToBounds = YES; //隱藏邊界
self.backgroundColor = kRandomColor;
//        [UIColor grayColor]
//顯示一個隨機驗證碼
[self changeCaptcha];
}
return self;
}
#pragma mark 更換驗證碼,得到更換的驗證碼的字串
-(void)changeCaptcha
{
//<一>從字元陣列中隨機抽取相應數量的字元,組成驗證碼字串
//陣列中存放的是全部可選的字元,可以是字母,也可以是中文
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];
//如果能確定最大需要的容量,使用initWithCapacity:來設定,好處是當元素個數不超過容量時,新增元素不需要重新分配記憶體
NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:kCharCount];
self.changeString = [[NSMutableString alloc] initWithCapacity:kCharCount];
//隨機從陣列中選取需要個數的字元,然後拼接為一個字串
for(int i = 0; i < kCharCount; i  )
{
NSInteger index = arc4random() % ([self.changeArray count] - 1);
getStr = [self.changeArray objectAtIndex:index];
self.changeString = (NSMutableString *)[self.changeString stringByAppendingString:getStr];
}
}
#pragma mark 點選view時呼叫,因為當前類自身就是UIView,點選更換驗證碼可以直接寫到這個方法中,不用再額外新增手勢
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//點選介面,切換驗證碼
[self changeCaptcha];
//setNeedsDisplay呼叫drawRect方法來實現view的繪製
[self setNeedsDisplay];
}
#pragma mark 繪製介面(1.UIView初始化後自動呼叫; 2.呼叫setNeedsDisplay方法時會自動呼叫)
- (void)drawRect:(CGRect)rect {
// 重寫父類方法,首先要呼叫父類的方法
[super drawRect:rect];
//設定隨機背景顏色
self.backgroundColor = kRandomColor;
//獲得要顯示驗證碼字串,根據長度,計算每個字元顯示的大概位置
NSString *text = [NSString stringWithFormat:@"%@",self.changeString];
CGSize cSize = [@"S" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.0]}];
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:kFontSize}];
}
//呼叫drawRect:之前,系統會向棧中壓入一個CGContextRef,呼叫UIGraphicsGetCurrentContext()會取棧頂的CGContextRef
CGContextRef context = UIGraphicsGetCurrentContext();
//設定畫線寬度
CGContextSetLineWidth(context, kLineWidth);
//繪製干擾的彩色直線
for(int i = 0; i < kLineCount; i  )
{
//設定線的隨機顏色
UIColor *color = kRandomColor;
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