目前,我正在开发一个应用程序,其中我有一个图像视图,其中图像不断
一段时间后发生变化。现在我希望我能够写一些文字或绘制任何符号
,simple line,cross line 表示在图像上绘图出现在图像视图中。表示我想
在图像上添加文本或绘制一些符号或线条等。我到处寻找但不能
找到任何解决方案。我该如何解决这个问题?提前致谢。
目前,我正在开发一个应用程序,其中我有一个图像视图,其中图像不断
一段时间后发生变化。现在我希望我能够写一些文字或绘制任何符号
,simple line,cross line 表示在图像上绘图出现在图像视图中。表示我想
在图像上添加文本或绘制一些符号或线条等。我到处寻找但不能
找到任何解决方案。我该如何解决这个问题?提前致谢。
你有两个选择。
UILabel
作为 imageView 的子视图,并将 UILabel 的背景颜色设置为清除颜色。实现以下方法,该方法将图像和文本作为参数并返回文本添加的图像。
//给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:NSASCIIStringEncoding];// "05/05/09";
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
//rotate text
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
参考:http: //iphonesdksnippets.com/post/2009/05/05/Add-text-to-image-%28UIImage%29.aspx
看看这是否有效:
//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:NSASCIIStringEncoding];// "05/05/09";
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
//rotate text
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}