ios4 - 在图像上写入多行

标签 ios4 uiimage

text1 是一个非常长的字符串,所以我想要多行支持。 我需要为此做什么?

    int w = img.size.width;
    int h = img.size.height;

    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];
    CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetRGBFillColor(context, 255,255,255,1);

    //CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(-M_PI/4));;
    CGContextShowTextAtPoint(context, w/3 + 10, 5, text, strlen(text));
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return [UIImage imageWithCGImage:imageMasked];

最佳答案

如果您不介意使用 UIKit 方法而不是 CGContext 方法来绘制文本,则可以使用 drawInRect:withFont:lineBreakMode:绘制文本,它将为您处理换行。请注意,您可以使用 UIGraphicsPushContextUIGraphicsPopContext从现有的 CGGraphicsRef 转到 UIKit 上下文,如果您的目标是早于 4.0 的 iOS,则必须在主线程上进行 UIKit 绘图。

关于ios4 - 在图像上写入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7011468/

相关文章:

iphone - UIWebView loadHTMLString 显示空白屏幕

iphone - "CGBitmapContextCreate: invalid data bytes/row"... 为什么 camera+ 过滤会打乱我的图像裁剪程序?

ios - 您将如何以及在何处使用 instantiateViewControllerWithIdentifier

iphone - 如何读取和编写像 iPhone 笔记应用程序一样工作的 plist

video - iOS:在播放过程中获取每一帧视频帧的内容

objective-c - 在设备上使用 imageNamed 失败,但在模拟器中有效

ios - 我如何在 iOS 上以编程方式为图像着色?

iphone - iOS5 AVFoundation 图片转视频

iphone - 如何以编程方式创建 KMZ 文件?

cocoa-touch - 如何将标签栏项目连接到操作?