生成带文本的UIImage
来源:互联网 发布:带有企业域名的邮箱 编辑:程序博客网 时间:2024/06/09 08:54
-(UIImage *)createTextImg:(NSString *)imgName text:(NSString *)text1{ UIImage* img = [UIImage imageNamed:imgName]; NSAssert(img, @""); int w = img.size.width; int h = img.size.height; float scale = img.scale; w *= scale; h *= scale; // 画图 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage); // 画文本 CTFontRef ctfont = CTFontCreateWithName(CFSTR("ArialMT"), 16 * scale, NULL); CGColorRef ctColor = [[UIColor whiteColor] CGColor]; CFStringRef keys[] = { kCTFontAttributeName,kCTForegroundColorAttributeName }; CFTypeRef values[] = { ctfont,ctColor}; CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFStringRef ctStr = CFStringCreateWithCString(nil, [text1 UTF8String], kCFStringEncodingUTF8); CFAttributedStringRef attrString = CFAttributedStringCreate(NULL,ctStr, attr); CTLineRef line = CTLineCreateWithAttributedString(attrString); CGContextSetTextMatrix(context, CGAffineTransformIdentity); CGContextSetTextPosition(context, 4 * scale, 22 * scale); CTLineDraw(line, context); CFRelease(line); CFRelease(attrString); CFRelease(ctStr); CGImageRef imageMasked = CGBitmapContextCreateImage(context); CGContextRelease(context); CGColorSpaceRelease(colorSpace); UIImage *image =[UIImage imageWithCGImage:imageMasked scale:scale orientation:img.imageOrientation]; CFRelease(imageMasked); return image;}
0 0
- 生成带文本的UIImage
- 【iOS学习笔记】生成带文本的UIImage
- 带边框的UIImage缩放
- 生成缩略图的UIImage分类
- iOS UIimage对象的生成
- 生成一个带文本提示信息的图片提交按钮
- cocos2d-x生成带边框的标签CCLabelTTF文本
- 带文本的标签自动生成font标签
- UIImage上绘制文本的方法
- 带边框的UIImage图片拉伸:resizableImageWithCapInsets
- mac下生成一个带滚动条浏览文本信息的窗口的方法
- 如何在EXCEL中批量生成前面带单引号的数字文本
- 如何在EXCEL中批量生成前面带单引号的数字文本和改变时间格式
- 制作带文本的按钮
- 带提示文本的ComboBox
- UIImage 图片生成
- UIColor生成UIImage
- 由UIColor 生成 UIImage
- 字符魔术——所见非所得
- <心跳连接一>关于信号有关的APUE和SIGALRM信号实例
- bootstrap-datetimepicker 的使用
- 终极版django, CSRF token missing or incorrect
- 类的特殊用法(二)super
- 生成带文本的UIImage
- Yii 结合CUrlManager和mod_rewrite的使用
- spread数据绑定设置
- MyEclipse插入作者邮件时间快捷键
- android monkey
- i.max6 e9 android系统添加永不休眠的选项
- 基于xml 实现动态加载权限功能树列表---EFSFrame企业级开发架构
- spread分组标修改方法
- PHP在变量前面加&是什么意思?