创建带文字的图片
2017-08-30 14:36
141 查看
创建带文字的图片
-(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, 44 * w, colorSpace, kCGImageAlphaPremultipliedFirst); CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);//将img绘至context上下文中 CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.5);//设置颜色 char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding]; CGContextSelectFont(context, "Georgia", 20, kCGEncodingMacRoman);//设置字体的大小 CGContextSetTextDrawingMode(context, kCGTextFill);//设置字体绘制方式 CGContextSetRGBFillColor(context, 1, 1, 1, 1);//设置字体绘制的颜色 if([text1 intValue]>=10){ CGContextShowTextAtPoint(context, w/2-strlen(text)*5, h/2-strlen(text)*4+2, text, strlen(text));//设置字体绘制的位置h/2-strlen(text)*4 }else{ CGContextShowTextAtPoint(context, w/2-strlen(text)*5, h/2-strlen(text)*4-2, text, strlen(text));//设置字体绘制的位置h/2-strlen(text)*4 } //Create image ref from the context CGImageRef imageMasked = CGBitmapContextCreateImage(context);//创建CGImage CGContextRelease(context); CGColorSpaceRelease(colorSpace); return [UIImage imageWithCGImage:imageMasked];//获得添加水印后的图片 }
相关文章推荐
- 代码创建button图片 文字等不显示或显示不正确原因及cell点击事件和btn的点击事件优先级
- Android ActionBar创建带图片和文字的menu
- php 文本文字创建生成图片
- 如何创建文字图片
- 论坛源码推荐(7月2日):用Swift实现的图片过渡效果 在应用内重新创建文字视图
- 用C#操作office组件库实现word文档的创建及文字 表格 图片的插入
- NO.3 创建一个图片,并在图片上画线插入文字等
- Qt自定义控件的创建与初步使用(二)之图片上绘制文字、箭头、曲线
- 创建富文本(文本可以包含文字,图片,自定义节点),c++,cocos2dx
- Java创建写入word文档图片和文字
- Android ActionBar创建带图片和文字的menu
- Qt自定义控件的创建与初步使用(二)之图片上绘制文字、箭头、曲线
- PHP的GD函数库图形类函数(创建缩略图、给图片加水印、给图片加文字、画布划线)
- Html 让文字显示在图片的上面
- python 把myinput.txt中的文字 分词,保存在myoutput.txt中,并制成图片
- Android Handler 异步消息处理机制的妙用 创建强大的图片加载类
- 传图取字:微信小程序自动把图片上的文字提取出来
- IOS在图片上画文字,非常简单
- 根据参数获取资源id,将图片代替指定文字进行显示
- 用java实现给图片增加图片水印或者文字水印(也支持视频图像帧添加水印)