-
呼如林
在图像中绘制文本并返回结果图像:+(UIImage*) drawText:(NSString*) text inImage:(UIImage*) image atPoint:(CGPoint) point { UIFont *font = [UIFont boldSystemFontOfSize:12]; UIGraphicsBeginImageContext(image.size); [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)]; CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height); [[UIColor whiteColor] set]; [text drawInRect:CGRectIntegral(rect) withFont:font]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage;}用法:// note: replace "ImageUtils" with the class where you pasted the method aboveUIImage *img = [ImageUtils drawText:@"Some text" inImage:img atPoint:CGPointMake(0, 0)];将图像内部文本的原点从0,0更改为您喜欢的任何点。要在文本后面绘制纯色矩形,请在行前添加以下内容[[UIColor whiteColor] set];:[[UIColor brownColor] set];CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, (image.size.height-[text sizeWithFont:font].height), image.size.width, image.size.height));我正在使用文本大小来计算纯色矩形的原点,但您可以用任何数字替换它。
-
慕容3067478
我对iOS 7支持的第一个答案的贡献:+(UIImage*) drawText:(NSString*) text inImage:(UIImage*) image atPoint:(CGPoint) point{ UIGraphicsBeginImageContextWithOptions(image.size, YES, 0.0f); [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)]; CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height); [[UIColor whiteColor] set]; UIFont *font = [UIFont boldSystemFontOfSize:12]; if([text respondsToSelector:@selector(drawInRect:withAttributes:)]) { //iOS 7 NSDictionary *att = @{NSFontAttributeName:font}; [text drawInRect:rect withAttributes:att]; } else { //legacy support [text drawInRect:CGRectIntegral(rect) withFont:font]; } UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage;}希望这可以帮助编辑:修改了UIGraphicsBeginImageContextWithOptions处理屏幕比例。
-
慕少森
这是Swift版本。func textToImage(drawText: NSString, inImage: UIImage, atPoint:CGPoint)->UIImage{ // Setup the font specific variables var textColor: UIColor = UIColor.whiteColor() var textFont: UIFont = UIFont(name: "Helvetica Bold", size: 12)! //Setup the image context using the passed image. UIGraphicsBeginImageContext(inImage.size) //Setups up the font attributes that will be later used to dictate how the text should be drawn let textFontAttributes = [ NSFontAttributeName: textFont, NSForegroundColorAttributeName: textColor, ] //Put the image into a rectangle as large as the original image. inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height)) // Creating a point within the space that is as bit as the image. var rect: CGRect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height) //Now Draw the text into an image. drawText.drawInRect(rect, withAttributes: textFontAttributes) // Create a new image out of the images we have created var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() // End the context now that we have the image we need UIGraphicsEndImageContext() //And pass it back up to the caller. return newImage}要调用它,您只需传入一个图像。textToImage("000", inImage: UIImage(named:"thisImage.png")!, atPoint: CGPointMake(20, 20))以下链接帮助我顺利完成。Swift - 使用drawInRect绘制文本:withAttributes:如何在Objective-C(iOS)中的图像上写文字?最初的目标是创建一个动态图像,我可以在AnnotaionView中使用它,例如在地图上的给定位置定价,这对它来说非常有用。希望这有助于某人尝试做同样的事情。