如何绘制文字+图像的UIGraphics?

时间:2013-06-09 11:22:26

标签: iphone ios uiimage ios5 uigraphicscontext

最近我买了一台支持iOS打印的热敏打印机,并有一个SDK。打印功能需要将内容光栅图像打印。但由于我不熟悉UIGrpahics,我不知道应该怎样做才能实现我想要打印的内容,所以下面是代码。我希望你们能告诉我如何让它发挥作用。

非常感谢你。

NSString *textToPrint = @"Hello dear,              \r\n"
                     "Welcome to my place    \r\n"
                     "\r\n"
                     "\r\n"
                     "Take a seat and I will be with you in a moment";

int width = 500;

NSString *fontName = @"Arial";

double fontSize = 12.0;

fontSize *= 2;

UIFont *font = [UIFont fontWithName:fontName size:fontSize];

CGSize size = CGSizeMake(width, 10000);
CGSize messuredSize = [textToPrint sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    if ([[UIScreen mainScreen] scale] == 2.0) {
        UIGraphicsBeginImageContextWithOptions(messuredSize, NO, 1.0);
    } else {
        UIGraphicsBeginImageContext(messuredSize);
    }
} else {
    UIGraphicsBeginImageContext(messuredSize);
}

CGContextRef ctr = UIGraphicsGetCurrentContext();
UIColor *color = [UIColor whiteColor];
[color set];

CGRect rect = CGRectMake(0, 0, messuredSize.width, messuredSize.height);
CGContextFillRect(ctr, rect);

color = [UIColor blackColor];
[color set];

[textToPrint drawInRect:rect withFont:font lineBreakMode:UILineBreakModeWordWrap];

UIImage *imageToPrint = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

我知道这会打印出前面的文字,但是我想在文本下面添加一个图像,我想我会用UIImage来做,但是我如何在文本下方绘制该图像?什么是“CGContextRef”呢?为什么需要填充白色,然后将“CGContextFillRect”填充为黑色?

谢谢。

0 个答案:

没有答案