iOS图文验证码的生成

demo下载地址
运行时,可能会出现:CGContextSetCompositeOperation: invalid context 0x0. If you want to see the backtrace, please set 的错误
在网上查了查,他们把锅都甩到了苹果官网,官方bug
有兴趣的可以自己看看

代码

//
//  IceAuthcode.m
//  自动生成验证码
//
//  Created by sanmi on 16/8/15.
//  Copyright © 2016年 sanmi. All rights reserved.
//  

#import "IceAuthcodeView.h"

#define color (arc4random() % 256)/255.0

@interface IceAuthcodeView ()

@property (nonatomic, strong) NSMutableString *code;
/*** 显示的随机字符串内容 **/
@property (nonatomic, strong) NSMutableArray *codeStrArr;
@property (nonatomic) CGContextRef context;

@end

@implementation IceAuthcodeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self getRandomStr];
        [self setBackgroundColor:[UIColor whiteColor]];
    }
    return self;
}
- (void)getRandomStr{
    _codeStrArr = [NSMutableArray array];
    CGFloat strCount = arc4random() % 2 + 4;
    NSArray *array = [[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 *mainStr = [NSMutableString string];
    for (int i = 0; i < strCount; i++) {
        NSString *codeStr = array[arc4random() % array.count];
        [_codeStrArr addObject: codeStr];
        [mainStr appendString:codeStr];
    }
    _randomStr = mainStr;
}
- (void)drawRect:(CGRect)rect
{
    // 清除
    CGContextClearRect(_context, rect);
    // 背景色
    CGContextSetFillColorWithColor(_context, [UIColor whiteColor].CGColor);
    CGContextFillRect(_context, rect);
    
    // 画线
    _context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(_context, 1);
    CGContextSetStrokeColorWithColor(_context, [UIColor lightGrayColor].CGColor);
    for (int i = 0; i < arc4random() % 5 + 2; i++)
    {
        // 线条起点坐标
        CGFloat x = arc4random() % (int)rect.size.width;
        CGFloat y = arc4random() % (int)rect.size.height;
        CGContextMoveToPoint(_context, x, y);
        
        x = arc4random() % (int)rect.size.width;
        y = arc4random() % (int)rect.size.height;
        CGContextAddLineToPoint(_context, x, y);
        
        CGContextStrokePath(_context);
    }
    self.code = [NSMutableString string];
    for (int i = 0; i < _codeStrArr.count; i ++)
    {
        NSString *str = _codeStrArr[i];
        [self.code appendString:str];
        NSArray *fontArray = [UIFont familyNames];
        NSDictionary *dict = @{NSFontAttributeName:[UIFont fontWithName:fontArray[arc4random() % (fontArray.count - 13)] size:25], NSForegroundColorAttributeName:[UIColor colorWithRed:color green:color blue:color alpha:1.0]};
        [str drawAtPoint:CGPointMake(i * (rect.size.width) / _codeStrArr.count + arc4random() % 3, 0) withAttributes:dict];
    }
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self getRandomStr];
    [self setNeedsDisplay];
}

@end

你可能感兴趣的:(iOS图文验证码的生成)