I want to write some text in a UIImage, I've found this link and it works, but if you try to write some special characters like "á, é, í, ö, ô..." then it fails.
The code:
//Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, 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];
}
Is there any idea to solve this issue?
Thank you.
EDIT:
I've tried to change:
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
to
char* text = (char *)[text1 cStringUsingEncoding:NSUTF8StringEncoding];
but it doesn't work.
Another user told me how to do it:
-(UIImage *)writeText:(NSString *)text1 toImage:(UIImage *)img{
CGSize sizeText = [text1 sizeWithFont:[UIFont fontWithName:#"Helvetica" size:36] minFontSize:36 actualFontSize:nil forWidth:783 lineBreakMode:UILineBreakModeTailTruncation];
CGFloat posX = 1024.0 - 230.0 - sizeText.width;
UIGraphicsBeginImageContextWithOptions(img.size, NO, 0.0f);
[img drawAtPoint:CGPointMake(0.0f, 0.0f)];
[text1 drawAtPoint:CGPointMake(posX, 588.0) withFont:[UIFont fontWithName:#"Helvetica" size:36]];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
In this link
try using NSUTF8StringEncoding instead of NSASCIIStringEncoding
Related
I just want to put the current geolocation on a photo taken from my iPhone/iPad.Is this possible?I have already got the geolocation but can I edit the photo programmatically by adding a text label over it.
Thanks for any help.
You can add text on image using :
//Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, 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];
}
Hope it helps you.
here is the sample method which will return you a image with text on it
-(UIImage*) getImageWithText:(UIImage*) sourceImage
{
UIGraphicsBeginImageContext(sourceImage.size);
[sourceImage drawAtPoint:CGPointZero];
[#"someText" drawAtPoint:CGPointMake(x,y) [UIFont systemFontOfSize:14]]
UIImage* imageWithText = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageWithText;
}
I'm trying to draw a NSString at the bottom of my CGRect. So far I've been able to have the text to be at the top centered but I need it to be positioned at the bottom. I also tried DrawAtPoint but that didn't work. Here's my code that I currently have:
UIGraphicsBeginImageContext(img.size);
CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:aRectangle];
[[UIColor whiteColor] set];
NSInteger fontSize = 25;
UIFont *font = [UIFont boldSystemFontOfSize: fontSize];
[text drawInRect:aRectangle withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
you can use the method:
sizeWithFont:constrainedToSize:lineBreakMode
get the text size, then calculate the left top point and use drawAtPoint to draw the text
Use this method:
-(UIImage *)imageFromText:(NSString *)text
{
CGSize maximumSize = CGSizeMake(300, 1000); //set width for string to wrap.
UIFont *font = [UIFont boldSystemFontOfSize:16];
CGSize strSize = [text sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(strSize,NO,0.0);
else
// iOS is < 4.0
UIGraphicsBeginImageContext(strSize);
CGRect newframe = CGRectMake(0, 0, strSize.width, strSize.height);
[text drawInRect:newframe
withFont:font
lineBreakMode:UILineBreakModeWordWrap
alignment:UITextAlignmentLeft];
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}
I found out that this code helped my problem extremely.
- (UIImage *)addText:(UIImage *)img text:(NSString *)text1{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, 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
CGContextShowTextAtPoint(context, 0, 5, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
I want to create UIImage of text.Is there any tutorial of something which could help me.I read many blog but all the blogs showing that how to create text on uiimage but I need uimage of written text. If somebody have any idea about this , please help me.Thank you very much.
hi you try this this....
i hope it will work for you
-(UIImage *)imageFromText:(NSString *)text withImage:(UIImage*)img{
// set the font type and size
UIFont *font = [UIFont systemFontOfSize:100.0];
CGSize size = [text sizeWithFont:font];
// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
else
// iOS is < 4.0
UIGraphicsBeginImageContext(size);
// optional: add a shadow, to avoid clipping the shadow you should make the context size bigger
//
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetShadowWithColor(ctx, CGSizeMake(0.0, 1.0), 5.0, [[UIColor blackColor] CGColor]);
CGContextSetBlendMode(ctx,kCGBlendModeNormal);
// CGContextSetFillColorWithColor(ctx, [UIColor whiteColor].CGColor);
CGContextSetFillColorWithColor(ctx, [UIColor colorWithPatternImage:img].CGColor);
/*NSLog(#"Rect %#",CGContextGetClipBoundingBox(ctx));
CGImageRef alphaMask = CGBitmapContextCreateImage(ctx);
CGContextClipToMask(ctx, CGContextGetClipBoundingBox(ctx), alphaMask);*/
// draw in context, you can use also drawInRect:withFont:
[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];
// transfer image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
the method call will be...
[self imageFromText:#"Booooooooom"];
Check this snippet of code, should work. You can freely change some of the parameters within function. Note that you must pass UIImage created somewhere else to this method.
//Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, 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];
}
This code is not working and i do not understand the mistake.
Please help.
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1
{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
/*UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
CGContextShowTextAtPoint(UIGraphicsGetCurrentContext(), 50, 100, text, strlen(text));
UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(#"View Image : %#",viewImage);*/
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, 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
CGContextShowTextAtPoint(context, 0, 50, text, strlen(text));
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
CGContextShowTextAtPoint(context, 50, 100, text, strlen(text));
//UIImage *compositeImage = UIGraphicsGetImageFromCurrentImageContext();
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
NSLog(#"Image : %#",[UIImage imageWithCGImage:imageMasked]);
return [UIImage imageWithCGImage:imageMasked];
//return viewImage;
}
Unless you have a reason to avoid NSString UIKit additions, this will suffice:
+(UIImage*) drawText:(NSString*) text
inImage:(UIImage*) image
atPoint:(CGPoint) point
font:(UIFont*) font
color:(UIColor*) color
{
UIGraphicsBeginImageContextWithOptions(image.size, FALSE, 0.0);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
[color set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Your code does work.
Make sure the string you are passing into it is not empty.
This is the code I am using to display the value of the slider inside my thumb image. But still the value is getting displayed below the slider. Please help me.
-(IBAction)sliderValueChanged:(id)sender
{
UISlider *aSlider = (UISlider *)sender;
[aSlider setThumbImage:
[self addText:self.thumbImage text:[NSString stringWithFormat:#"%.0f",
aSlider.value * 100]] forState:aSlider.state];
}
//Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
int w = img.size.width;
int h = img.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w,
colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
char* text= (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
CGContextSelectFont(context, "Arial",12, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 0, 0, 0, 1);
CGContextShowTextAtPoint(context,3,8,text, strlen(text));
CGImageRef imgCombined = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage *retImage = [UIImage imageWithCGImage:imgCombined];
CGImageRelease(imgCombined);
return retImage;