c语言中写汉字到图片上,如何在Objective-C(iOS)中的图像上写文字?

在图像内绘制文本并返回结果图像:

+(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 above

UIImage *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));

我正在使用文本大小来计算纯色矩形的原点,但是您可以将其替换为任何数字。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值