iOS 图片验证码绘制

作者: 七分呗轻唱 | 来源:发表于2016-03-18 13:38 被阅读3352次

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

Simulator Screen Shot 2016年3月18日 下午1.37.17.png

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 图片验证码绘制

    登录注册时用的验证码效果图 ViewDidload调用即可 } @endgithub下载地址

  • iOS 绘制image

    iOS 绘制图片

  • SDWebimage加载图片请求添加header

    后台添加了图片验证码请求的header验证:导致验证码图片加载不出来。iOS端需要添加代码如下:

  • GeekBand iOS Introductory 4. Ima

    两种界面美化方法:图片绘制,代码绘制 图片绘制 ios设备分辨率多样性 AssertCatalog:可视化管理针对...

  • iOS 绘制图片随机验证码

    .h文件 .m文件 VIewController中调用 手势事件

  • iOS-图片解压缩相关

    SDWebimage中对图片的解压缩 谈谈 iOS 中图片的解压缩 iOS 开发:绘制像素到屏幕 探讨iOS 中图...

  • 验证码

    绘制验证码: 修改验证码类中的绘制方法(参数顺序不能改变) 在模板输出验证码: 点击刷新验证码: 1)、添加点击事...

  • iOS本地动态验证码生成

    用于ios本地动态生成验证码,效果如下: 导入CoreGraphics.framework用于绘制图形 封装UIV...

  • iOS图片绘制渲染,View渲染

    iOS图片绘制的过程:CPU和GPU相互协作一.CPU计算frame,图片的解码,通过数据总线将需要绘制的纹理交给...

  • 2018-07-04

    后台绘制圆角图片 参考链接链接 iOS 离屏渲染优化(Offscreen Render)

网友评论

  • 丶茕茕丶:楼主。 判断输入跟图片里面的文字相同如何判断? codeLabel那个属性?

本文标题:iOS 图片验证码绘制

本文链接:https://www.haomeiwen.com/subject/jjzclttx.html