一个关于字符串打印到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];
}