Using IOS to draw pictures to generate random Captcha sample code _ios

Source: Internet
Author: User

Let's take a look at the effect chart.

Implementation methods

. h Files

@property (nonatomic, retain) Nsarray *changearray;
@property (nonatomic, retain) nsmutablestring *changestring;
@property (nonatomic, retain) Uilabel *codelabel;

-(void) ChangeCode;
@end

. m file

@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 < 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

Called in Viewcontroller

_codeview = [[CodeView alloc] Initwithframe:cgrectmake (15+ (screen_width-30)/3*2, (screen_width-30)/3, M)];
 Gesture
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Tapclick :)];
[_codeview Addgesturerecognizer:tap];
[Self.view addsubview: _codeview];

Gesture Events

-(void) Tapclick: (uitapgesturerecognizer*) Tap
{
  [_codeview ChangeCode];
}

Summarize

The above is the use of iOS to draw pictures random verification code of the entire content, I hope this article on the content of iOS developers can help, if you have questions you can message exchange.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.