利用iOS绘制图片生成随机验证码示例代码_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开发者们能有所帮助,如果有疑问大家可以留言交流。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索ios
, 随机生成验证码
, ios随机验证码
绘制图片
体温单绘制高清示例图、h5 验证码页面示例、验证码示例、canvas 验证码绘制、php绘制验证码,以便于您获取更多的相关知识。

时间: 2024-08-01 14:03:51

利用iOS绘制图片生成随机验证码示例代码_IOS的相关文章

iOS实现爆炸的粒子效果示例代码_IOS

照例我们先看看效果图 怎么样?效果很不错吧,下面来一起看看实现的过程和代码示例. 实现原理 从图中可以大致看出,爆炸点点都是取的某坐标的颜色值,然后根据一些动画效果来完成的. 取色值 怎么取的view的某个点的颜色值呢?google一下,就可以找到很多答案.就不具体说了.创建1*1的位图,然后渲染到屏幕上,然后得到RGBA.我这里写的是UIView的extension. extension UIView { public func colorOfPoint(point:CGPoint) -> U

IOS设置按钮为圆角的示例代码_IOS

iOS中很多时候都需要用到指定风格的圆角按钮,以下是UIButton提供的创建圆角按钮方法 设置按钮的4个角:      左上:UIRectCornerTopLeft      左下:UIRectCornerBottomLeft      右上:UIRectCornerTopRight      右下:UIRectCornerBottomRight 示例代码: UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 60

asp.net(C#) 生成随机验证码的代码_实用技巧

常用的生成验证码程序 ,图片效果如下:    源程序如下: 复制代码 代码如下: using System;  using System.IO;  using System.Drawing;  using System.Drawing.Imaging;  using System.Text;  using System.Collections;  using System.Web;  using System.Web.UI;  using System.Web.UI.WebControls; 

iOS实现动态的开屏广告示例代码_IOS

一.实现效果图 二.实现思路: 用一个固定的png图片左启动图,应该和广告视图需要进行动画的期初的位置一致,当启动图消失的时候,呈现出图片,实际遇到的困难是,因为广告图片是从网络请求加载的,当时把广告视图放在了请求数据的块里面,广告出现的时候会闪一下,放在外面就没事了. 三.实现示例 1.广告的头文件 // XBAdvertView.h // scoreCount // // Created by 王国栋 on 15/12/22. // Copyright 2015年 xiaobai. All

Android自定义控件深入学习 Android生成随机验证码_Android

在上一篇的文章中介绍了自定义控件的属性,详情见<详解Android自定义控件属性TypedArray以及attrs>.那么在这基础上实现随机验证码生成,里面的代码是自定义控件以及涉及到自定义view绘画.1.先看实现的效果图 看到这个效果图是不是感觉还可以.那么就看看源码吧.2.attr文件 <?xml version="1.0" encoding="utf-8"?> <resources> <attr name="

Android自定义控件深入学习 Android生成随机验证码

在上一篇的文章中介绍了自定义控件的属性,详情见<详解Android自定义控件属性TypedArray以及attrs>.那么在这基础上实现随机验证码生成,里面的代码是自定义控件以及涉及到自定义view绘画. 1.先看实现的效果图 看到这个效果图是不是感觉还可以.那么就看看源码吧. 2.attr文件 <?xml version="1.0" encoding="utf-8"?> <resources> <attr name=&qu

ASP.NET 生成随机验证码

我一直觉得用第三方控件生成的验证码太花了,用户体验不好,有的很难看清楚到底是什么,还是那种比较清楚一点的给人的感觉好点.    /// <summary>    /// 这个方法用来生成随机验证码    /// </summary>    private void ShowCode()    {        Random ran = new Random();        int intRandom = ran.Next(10001, 99999);         //将随机

ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)

asp.net|示例|下载|验证码 学习整理了一下(一).功能       用HttpHandler实现图片验证码       (二).代码如下  1. 处理程序文件 ValidateImageHandler.ashx代码如下  1 <%@ WebHandler Language="C#" Class="ValidateImageHandler" %> 2  3 using System; 4 using System.Web; 5 using Syst

PHP 生成英文单词验证码程序代码

具体  代码如下 复制代码 $width=145; $height = 45;          $authcode = vcaptcha_read_code('words.txt') ;          $bg = 'bg/captcha_bg3.jpg';          $img_type   = 'png';          /* 验证码长度 */ $letters = strlen($authcode);          $img_bg    = (function_exist