(Réglé) iOS Captcha, texte qui ne s'affiche pas
Bonjour à tous
- (void)viewDidLoad
{
[super viewDidLoad];
_emailTxt.placeholder = @Votre e-mail;
_bouton.layer.cornerRadius=10;
_emailTxt.returnKeyType = UIReturnKeyDone;
_emailTxt.delegate = (id)self;
UIBarButtonItem *refresh = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];
self.navigationItem.rightBarButtonItem = refresh;
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(back)];
NSString *alphabet = @ABCDEFGHIJKLMNOPQRSTUVWXZY0123456789;
NSMutableString* randomString = [NSMutableString stringWithCapacity:5];
for (int i = 0; i < 5; i++) {
[randomString appendFormat:@%C, [alphabet characterAtIndex:arc4random() %[alphabet length]]];
}
NSLog(@%@", randomString);
//UIImage* imageCaptacha = [self addText:[UIImage imageNamed:@img.png] text:randomString];
_captchaImg.image=[self addText:[UIImage imageNamed:@img.png] text:randomString];
}
-(UIImage *)addText:(UIImage *)img text:(NSMutableString *)text1{
int w = img.size.width;
int h = img.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// "05/05/09";
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
//rotate text
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
Voila il m'affiche bien mon image dans l'imageView, mais il ne m'affiche pas le texte, avez vous une idée svp ?
Connectez-vous ou Inscrivez-vous pour répondre.
Réponses
Mais l'écriture se fait en blanc. img.png une image "blanche"?
Effectivement oui c'était en blanc et le 1er caractère "sort" de l'image.
Une idée pour régler ce souci s'il vous plait ?
c'est parce que tu fais une rotation à Pi/4
Hors, ensuite tu le redéplaces, mais j'suis pas sûr que cela la bonne formule avec la rotation.
Bien vu
Si je veut mettre une UIFont intégrée au projet est ce que c'est un simple [UIFont FontWithName "nom.ttf"] ?
J'ai essayé de suivre ça, mais sans succès
http://stackoverflow.com/questions/14376941/how-to-use-custom-fonts-in-iphone-sdk
J'ai aussi essayé ça qui ne fonctionne pas