问题描述
我有一个文本框,我希望将书面文本添加到UIImage。
I have a textbox, where i want the written text to be added to a UIImage.
如何将NSString绘制为UIImage?
How can i draw NSString to a UIImage?
我搜索了一下,发现了很多例子,但是没有一个例子。 Xcode只给了我很多错误。
I´ve searched, and found lots of examples, but non of them works. Xcode just gives me lots of errors.
简单地说,我想在UIimage中绘制一个NSString。 UIImage应该与预定义的UIImageView相同,并放在中心。
Simply put, i want to draw a NSString to a UIimage. The UIImage should be the same size as a predefined UIImageView, and be placed in center.
任何想法?
推荐答案
我认为解决方案在这里......我在我的一个应用程序中使用了这个方法
这里lbltext是我的标签的对象。这种方法用给定的文本创建新图像&用文本返回新图像的对象。
i think solution is here..i have used this method in one of my applicationhere lbltext is the object of my label. this method creates new image with given text & return the object of new image with text.
-(UIImage *) drawText:(NSString*) text inImage:(UIImage*)image atPoint:(CGPoint)point
{
UIFont *font = lblText.font;
UIGraphicsBeginImageContext(iv.frame.size);
[image drawInRect:CGRectMake(0,0,iv.frame.size.width,iv.frame.size.height)];
CGRect rect = CGRectMake(point.x,point.y,lblText.frame.size.width, lblText.frame.size.height);
[lblText.textColor set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
在您的下面评论中,您已通过iv.center作为一个点..尝试manualy喜欢这个
in your below comment you have passed iv.center as a point.. try manualy like this
CGPoint point;
point.x=30; // dynamicaly just for example lblText.frame.origin.x;
point.y=40; //lblText.frame.origin.y;
你必须像这样调用上述方法......
you have to call above method like this...
UIImage *img = [self drawText:@"Test String" inImage:originalImage atPoint:point];
这里,img是用于存储带图文的新图像的UIImage的另一个实例...在调用此方法之后使用img imager进一步使用..
here,img is another instace of UIImage for storing new image with text...after calling this method use img imager for your further use..
例如......
[newimageviewObj setImage:img];
我希望这能帮到你
这篇关于绘制文本并添加到UIImage iOS 5/6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!