最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用iOS繪制圖片生成隨機驗證碼示例代碼

 更新時間:2016年11月13日 11:50:42   投稿:daisy  
驗證碼的功能一般是防止使用程序惡意注冊、暴力破解或批量發(fā)帖而設置的。所謂驗證碼,就是將一串隨機產(chǎn)生的數(shù)字或符號,生成一幅圖片,圖片里加上一些干擾象素(防止OCR),圖片驗證碼相信大家都見到過,這篇文章用示例代碼給大家介紹iOS繪制圖片生成隨機驗證碼的方法。

先來看看效果圖

實現(xiàn)方法

.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中調(diào)用

_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];
}

總結(jié)

以上就是利用iOS繪制圖片隨機驗證碼的全部內(nèi)容,希望本文的內(nèi)容對各位iOS開發(fā)者們能有所幫助,如果有疑問大家可以留言交流。

相關文章

  • iOS開發(fā)之自定義圖片拉伸功能

    iOS開發(fā)之自定義圖片拉伸功能

    這篇文章主要介紹了iOS開發(fā)之自定義圖片拉伸功能,需要的朋友可以參考下
    2017-06-06
  • iOS仿擦玻璃效果的實現(xiàn)方法

    iOS仿擦玻璃效果的實現(xiàn)方法

    最近在網(wǎng)上看到一個博客分享的這個效果很不錯,就拿下來看看,結(jié)果看了好幾遍也沒完全看懂,再結(jié)合自己之前學的東西感覺不用這么復雜也能實現(xiàn)同樣的效果,于是就開始動手了?,F(xiàn)在將實現(xiàn)的步驟和示例代碼分享給大家,有需要的朋友們可以參考借鑒。
    2016-10-10
  • IOS 開發(fā)之swift中UIView的擴展使用的實例

    IOS 開發(fā)之swift中UIView的擴展使用的實例

    這篇文章主要介紹了IOS 開發(fā)之swift中UIView的擴展使用的實例的相關資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • 詳解iOS自定義UITabBar與布局

    詳解iOS自定義UITabBar與布局

    本篇文章給大家詳細分析了iOS自定義UITabBar與布局的實際操作過程以及相關代碼分享,一起學習下。
    2018-02-02
  • iOS實現(xiàn)全局懸浮按鈕

    iOS實現(xiàn)全局懸浮按鈕

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)全局懸浮按鈕,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • ios 貝塞爾曲線切割圓角的方法

    ios 貝塞爾曲線切割圓角的方法

    本篇文章主要介紹了ios 貝塞爾曲線切割圓角的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • 關于iOS GangSDK的使用 為App快速集成社群公會模塊

    關于iOS GangSDK的使用 為App快速集成社群公會模塊

    這篇文章主要介紹了iOS GangSDK的使用為App快速集成社群公會模塊功能的實現(xiàn)過程。
    2017-11-11
  • IOS給xcode工程關聯(lián)pod的實例詳解

    IOS給xcode工程關聯(lián)pod的實例詳解

    這篇文章主要介紹了IOS給xcode工程關聯(lián)pod的實例詳解的相關資料,希望大家通過本文能實現(xiàn)這樣的需求,需要的朋友可以參考下
    2017-09-09
  • Flutter?GetPageRoute實現(xiàn)嵌套導航學習

    Flutter?GetPageRoute實現(xiàn)嵌套導航學習

    這篇文章主要為大家介紹了Flutter?GetPageRoute實現(xiàn)嵌套導航的示例學習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • iOS開發(fā)之枚舉用法小結(jié)

    iOS開發(fā)之枚舉用法小結(jié)

    大家都知道枚舉是C語言中的一種基本數(shù)據(jù)類型,是一個"被命名的整型常量"的集合,它不參與內(nèi)存的占用和釋放,我們在開發(fā)中使用枚舉的目的只有一個,那就是為了增加代碼的可讀性。下面就來來看看在iOS中枚舉的用法,有需要的朋友們可以看看。
    2016-09-09

最新評論

县级市| 凤台县| 东乌珠穆沁旗| 麻栗坡县| 哈密市| 吴旗县| 九龙县| 淄博市| 江油市| 盘锦市| 遵义县| 鹤岗市| 顺昌县| 大厂| 连城县| 余庆县| 利辛县| 保靖县| 库尔勒市| 小金县| 永仁县| 河津市| 吴桥县| 桐城市| 休宁县| 塔河县| 阜城县| 九龙城区| 舟山市| 瑞昌市| 延寿县| 长武县| 霍城县| 和政县| 独山县| 岳阳县| 且末县| 宁波市| 福海县| 漠河县| 江安县|