【iOS学习笔记】生成带文本的UIImage
来源:互联网 发布:一个月工资3000 知乎 编辑:程序博客网 时间:2024/06/14 14:20
-(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
- 【iOS学习笔记】生成带文本的UIImage
- 生成带文本的UIImage
- iOS UIimage对象的生成
- ios学习临时笔记-UIImage拉伸
- iOS:NSString生成UIImage
- iOS学习笔记-033.数据的读取——NSString和UIImage的保存
- ios学习笔记(二)UIImage两种初始化的区别
- ios 利用颜色生成UIImage
- ios生成的UIimage获取img像素信息
- 带边框的UIImage缩放
- IOS开发之带格式的文本
- 【iOS初学笔记】翻转UIImage
- 【LDA学习笔记】文本生成方式
- UIImage的用法笔记
- iOS: 学习笔记, 添加一个带界面约束的控制器
- ios学习--自己画UIImage
- 生成缩略图的UIImage分类
- ios从rgb数据生成UIImage
- 把Testlink的case导出转成excel,或把excel导入Testlink
- 判断windows 关闭或者隐藏的状态
- Unity项目工程集成Mob社会化分享SDK(android篇)
- IOS开发-四种保存数据的方式
- 南邮 OJ 1574 男女配对
- 【iOS学习笔记】生成带文本的UIImage
- 河内之塔(汉诺塔)
- 霧島レイNavigates 沢城みゆきのRadio Drive 泽城美雪
- 实战篇:finereport如何搭建机电行业综合管理平台
- HTML5 Web 存储功能
- HDOJ 1242 Rescue(bfs+优先队列)
- dbus1.0.2、libxm2交叉编译、安装
- Maven插件开发
- filter的配置和filter模板