iOS 随机图片验证码

来源:互联网 发布:淘宝店铺pc端首页多高 编辑:程序博客网 时间:2024/06/05 22:42
  1. - (void)onTapToGenerateCode:(UITapGestureRecognizer *)tap {  
  2.   for (UIView *view in self.checkCodeNumberLabel.subviews) {  
  3.     [view removeFromSuperview];  
  4.   }  
  5.   // @{  
  6.   // @name 生成背景色  
  7.   float red = arc4random() % 100 / 100.0;  
  8.   float green = arc4random() % 100 / 100.0;  
  9.   float blue = arc4random() % 100 / 100.0;  
  10.   UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:0.2];  
  11.   [self.checkCodeNumberLabel setBackgroundColor:color];  
  12.   // @} end 生成背景色  
  13.     
  14.   // @{  
  15.   // @name 生成文字  
  16.   const int count = 5;  
  17.   char data[count];  
  18.   for (int x = 0; x < count; x++) {  
  19.     int j = '0' + (arc4random_uniform(75));  
  20.     if((j >= 58 && j <= 64) || (j >= 91 && j <= 96)){  
  21.       --x;  
  22.     }else{  
  23.       data[x] = (char)j;  
  24.     }  
  25.   }  
  26.   NSString *text = [[NSString alloc] initWithBytes:data  
  27.                                             length:count encoding:NSUTF8StringEncoding];  
  28.   self.code = text;  
  29.   // @} end 生成文字  
  30.     
  31.   CGSize cSize = [@"S" sizeWithFont:[UIFont systemFontOfSize:16]];  
  32.   int width = self.checkCodeNumberLabel.frame.size.width / text.length - cSize.width;  
  33.   int height = self.checkCodeNumberLabel.frame.size.height - cSize.height;  
  34.   CGPoint point;  
  35.   float pX, pY;  
  36.   for (int i = 0, count = text.length; i < count; i++) {  
  37.     pX = arc4random() % width + self.checkCodeNumberLabel.frame.size.width / text.length * i - 1;  
  38.     pY = arc4random() % height;  
  39.     point = CGPointMake(pX, pY);  
  40.     unichar c = [text characterAtIndex:i];  
  41.     UILabel *tempLabel = [[UILabel alloc]  
  42.                           initWithFrame:CGRectMake(pX, pY,  
  43.                                                    self.checkCodeNumberLabel.frame.size.width / 4,  
  44.                                                    self.checkCodeNumberLabel.frame.size.height)];  
  45.     tempLabel.backgroundColor = [UIColor clearColor];  
  46.       
  47.     // 字体颜色  
  48.     float red = arc4random() % 100 / 100.0;  
  49.     float green = arc4random() % 100 / 100.0;  
  50.     float blue = arc4random() % 100 / 100.0;  
  51.     UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];  
  52.   
  53.     NSString *textC = [NSString stringWithFormat:@"%C", c];  
  54.     tempLabel.textColor = color;  
  55.     tempLabel.text = textC;  
  56.     [self.checkCodeNumberLabel addSubview:tempLabel];  
  57.   }  
  58.     
  59.   // 干扰线  
  60.   CGContextRef context = UIGraphicsGetCurrentContext();  
  61.   CGContextSetLineWidth(context, 1.0);  
  62.   for(int i = 0; i < count; i++) {  
  63.     red = arc4random() % 100 / 100.0;  
  64.     green = arc4random() % 100 / 100.0;  
  65.     blue = arc4random() % 100 / 100.0;  
  66.     color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];  
  67.     CGContextSetStrokeColorWithColor(context, [color CGColor]);  
  68.     pX = arc4random() % (int)self.checkCodeNumberLabel.frame.size.width;  
  69.     pY = arc4random() % (int)self.checkCodeNumberLabel.frame.size.height;  
  70.     CGContextMoveToPoint(context, pX, pY);  
  71.     pX = arc4random() % (int)self.checkCodeNumberLabel.frame.size.width;  
  72.     pY = arc4random() % (int)self.checkCodeNumberLabel.frame.size.height;  
  73.     CGContextAddLineToPoint(context, pX, pY);  
  74.     CGContextStrokePath(context);  
  75.   }  
  76.   return;  
  77. }  


http://blog.csdn.net/woaifen3344/article/details/25731305
0 0