作者LifeTek (*****)
看板MacDev
标题[请益] 请问文字跟图片整合问题
时间Thu Jun 13 23:58:04 2013
Dear 各位高手
不知有人有用过将文字的字串跟图片合成,
然後成为另一个图片有合成文字的功能吗,
我有试着用如下的code, 英文可以,但是中文会出现乱码
主要原因是编码好像没有支援中文,不知有人有解过这个问题吗
请多指教,code如下:
//Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char* text = (char *)[text1 cStringUsingEncoding:NSUTF8StringEncoding];// "05/05/09";
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
CGContextShowTextAtPoint(context, 4, 10, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 1.163.83.225