读书人

一个关于字符串打印到GraphicsContext

发布时间: 2013-08-16 14:29:57 作者: rapoo

一个关于字符串打印到GraphicsContext上的问题


+ (void)generatePDFByImg:(UIImage *)img andText:(NSString *)text
{
// self.toolBar.hidden = YES;
// NSString *text = @"puma are large cat-like animals which are found in Americ";

CGSize pdfSize;
pdfSize.height = 460;
pdfSize.width = 320;

UIGraphicsBeginImageContext([[UIScreen mainScreen] bounds].size);
// [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
// UIImage *pdfImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
// self.iiimagev.image = pdfImage;

// UIImage *pdfImage = [UIImage imageNamed:@"10074136.jpg"];

NSMutableData* outputData = [[NSMutableData alloc] init];
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)outputData);
CFMutableDictionaryRef attrDictionary = NULL;
attrDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, NULL, attrDictionary);
CFRelease(dataConsumer);
CFRelease(attrDictionary);
UIImage* myUIImage = img;
CGImageRef pageImage = [myUIImage CGImage];
CGPDFContextBeginPage(pdfContext, NULL);
NSLog(@"[myUIImage size].width = %f",[myUIImage size].width);
NSLog(@"[myUIImage size].height = %f",[myUIImage size].height);
CGContextDrawImage(pdfContext, CGRectMake(0,[[UIScreen mainScreen] bounds].size.height,([myUIImage size].width)*2 , ([myUIImage size].height)*2), pageImage);
// NSLog(@"[myUIImage size].width = %f",[myUIImage size].width);
// NSLog(@"[myUIImage size].height = %f",[myUIImage size].height);


// CGContextDrawImage(pdfContext, CGRectMake(0, 0, [myUIImage size].width, [myUIImage size].height), pageImage);
// CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
// [text drawInRect:CGRectMake([myUIImage size].width, [myUIImage size].width, 30, 80) withFont:[UIFont boldSystemFontOfSize:15]];

NSString *str = [NSString stringWithFormat:@"pulma is lager cat-like animals:%@ seconds", @"65555"];

//选择字体为16pt Helvetica
CGContextSelectFont(pdfContext, "Helvetica", 16, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(pdfContext, kCGTextFill); //设置绘制模式
//设置文本颜色为黑色
CGContextSetRGBFillColor(pdfContext, 0.0, 0.0, 200.0, 1.0);
//转换str为一个C 字符串并显示它

CGContextShowTextAtPoint(pdfContext, 10 , 10, [str cStringUsingEncoding:[NSString defaultCStringEncoding]], str.length);

// CGContextRestoreGState(pdfContext); //恢复上下文

CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
CGContextRelease(pdfContext);
// NSString* pdfFileName = [self getPDFFileName];
NSString *pdfFileName = [NSHomeDirectory() stringByAppendingPathComponent: @"mytestpdf.pdf"];
[outputData writeToFile:pdfFileName atomically:YES];

}


这段代码实现将一个图片和一些文字生成到PDF中,使用CGContextShowTextAtPoint(pdfContext, 10 , 10, [str cStringUsingEncoding:[NSString defaultCStringEncoding]], str.length);
这个方法可以生成英文,但是无法生成中文,我就想用 [str drawAtPoint:point withFont:[UIFont systemFontOfSize:16]]来实现,但是调用之后PDF中并没有出现文字
哪位大神懂?
------解决方案--------------------


一个关于字符串打印到GraphicsContext上的有关问题,厚脸接个分
[解决办法]
求分享.....

读书人网 >Iphone

热点推荐