怎么在iOS中實(shí)現(xiàn)生成圖片數(shù)字字母驗(yàn)證效果-創(chuàng)新互聯(lián)

這篇文章給大家介紹怎么在iOS中實(shí)現(xiàn)生成圖片數(shù)字字母驗(yàn)證效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的竹山網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
#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; //設(shè)置layer圓角半徑
    self.layer.masksToBounds = YES; //隱藏邊界
    self.backgroundColor = kRandomColor;

    //    [UIColor grayColor]

    //顯示一個(gè)隨機(jī)驗(yàn)證碼
    [self changeCaptcha];
  }

  return self;
}
#pragma mark 更換驗(yàn)證碼,得到更換的驗(yàn)證碼的字符串
-(void)changeCaptcha
{
  //<一>從字符數(shù)組中隨機(jī)抽取相應(yīng)數(shù)量的字符,組成驗(yàn)證碼字符串
  //數(shù)組中存放的是全部可選的字符,可以是字母,也可以是中文
  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:來設(shè)置,好處是當(dāng)元素個(gè)數(shù)不超過容量時(shí),添加元素不需要重新分配內(nèi)存
  NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:kCharCount];
  self.changeString = [[NSMutableString alloc] initWithCapacity:kCharCount];

  //隨機(jī)從數(shù)組中選取需要個(gè)數(shù)的字符,然后拼接為一個(gè)字符串
  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 點(diǎn)擊view時(shí)調(diào)用,因?yàn)楫?dāng)前類自身就是UIView,點(diǎn)擊更換驗(yàn)證碼可以直接寫到這個(gè)方法中,不用再額外添加手勢
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  //點(diǎn)擊界面,切換驗(yàn)證碼
  [self changeCaptcha];

  //setNeedsDisplay調(diào)用drawRect方法來實(shí)現(xiàn)view的繪制
  [self setNeedsDisplay];
}

#pragma mark 繪制界面(1.UIView初始化后自動(dòng)調(diào)用; 2.調(diào)用setNeedsDisplay方法時(shí)會自動(dòng)調(diào)用)
- (void)drawRect:(CGRect)rect {
  // 重寫父類方法,首先要調(diào)用父類的方法
  [super drawRect:rect];

  //設(shè)置隨機(jī)背景顏色
  self.backgroundColor = kRandomColor;

  //獲得要顯示驗(yàn)證碼字符串,根據(jù)長度,計(jì)算每個(gè)字符顯示的大概位置
  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;

  //依次繪制每一個(gè)字符,可以設(shè)置顯示的每個(gè)字符的字體大小、顏色、樣式等
  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}];
  }

    //調(diào)用drawRect:之前,系統(tǒng)會向棧中壓入一個(gè)CGContextRef,調(diào)用UIGraphicsGetCurrentContext()會取棧頂?shù)腃GContextRef
    CGContextRef context = UIGraphicsGetCurrentContext();
    //設(shè)置畫線寬度
    CGContextSetLineWidth(context, kLineWidth);

    //繪制干擾的彩色直線
    for(int i = 0; i < kLineCount; i++)
    {
      //設(shè)置線的隨機(jī)顏色
      UIColor *color = kRandomColor;
      CGContextSetStrokeColorWithColor(context, [color CGColor]);
      //設(shè)置線的起點(diǎn)
      pX = arc4random() % (int)rect.size.width;
      pY = arc4random() % (int)rect.size.height;
      CGContextMoveToPoint(context, pX, pY);
      //設(shè)置線終點(diǎn)
      pX = arc4random() % (int)rect.size.width;
      pY = arc4random() % (int)rect.size.height;
      CGContextAddLineToPoint(context, pX, pY);
      //畫線
      CGContextStrokePath(context);
    }
}
@end

關(guān)于怎么在iOS中實(shí)現(xiàn)生成圖片數(shù)字字母驗(yàn)證效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

標(biāo)題名稱:怎么在iOS中實(shí)現(xiàn)生成圖片數(shù)字字母驗(yàn)證效果-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:http://muchs.cn/article10/ddjhgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、做網(wǎng)站、小程序開發(fā)網(wǎng)頁設(shè)計(jì)公司、App設(shè)計(jì)定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)