今天的项目中需要在PDF的中创建一些表格和雷达图。
先放最后的效果图:
1、先说雷达图,有两个比较好用的分别是BTSpiderPlotterView和JYRadarChart
这两个库的使用都非常简单,属于那种一看就会的,详情就不说了。
代码:
radarChartView = [[JYRadarChart alloc] initWithFrame:CGRectMake(0, 220, 320, 200)];
radarChartView.showLegend = YES;
// [radarChartView setTitles:@[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j"]];
radarChartView.drawPoints = YES;
radarChartView.attributes = @[@"展厅", @"客休区", @"精品展示区", @"服务部办公室", @"服务前台", @"公示墙",
@"接车通道", @"调度室", @"维修车间", @"备件部", @"待交区", @"新车交车区", @"收银处", @"客户运营中心", @"会议室", @"酒店"];
NSArray *b1 = @[@(100), @(50), @(100), @(78), @(80), @(100), @(78), @(100), @(100), @(100), @(100), @(100), @(100), @(50), @(70), @(50)];
radarChartView.dataSeries = @[b1];
[radarChartView setColors:@[[UIColor greenColor]]];
radarChartView.steps = 5;
radarChartView.backgroundColor = [UIColor clearColor];
[self.view addSubview:radarChartView];
注意数组的数量统一就好。
效果:
2、截图保存
建立好雷达图之后
if ([radarChartView isKindOfClass:NSClassFromString(@"JYRadarChart")]) {//找到自己需要的subView
NSLog(@"fund");
//支持retina高分的关键
if(UIGraphicsBeginImageContextWithOptions != NULL)
{
UIGraphicsBeginImageContextWithOptions(radarChartView.frame.size, NO, 0.0);
} else {
UIGraphicsBeginImageContext(radarChartView.frame.size);
}
//获取图像
[radarChartView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//保存图像
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *filepath = [path stringByAppendingString:@"/0.png"];
if ([UIImagePNGRepresentation(image) writeToFile:filepath atomically:YES]) {
// index += 1;
NSLog(@"Succeeded! %@",filepath);
[self createPdf];
}
else {
NSLog(@"Failed!");
}
}
然后就是需要把之前的图片加到PDF里面
-(void)createPdf{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
NSString *saveFileName = @"myPDF.pdf";
NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
const char *filename = [newFilePath UTF8String];
CGRect pageRect=CGRectMake(0, 0, 1099, 714);
// This code block sets up our PDF Context so that we can draw to it
// CGContextRef pdfContext;
CFStringRef path;
CFURLRef url;
CFMutableDictionaryRef myDictionary = NULL;
// Create a CFString from the filename we provide to this method when we call it
path = CFStringCreateWithCString (NULL, filename,
kCFStringEncodingUTF8);
// Create a CFURL using the CFString we just defined
url = CFURLCreateWithFileSystemPath (NULL, path,
kCFURLPOSIXPathStyle, 0);
CFRelease (path);
// This dictionary contains extra options mostly for ‘signing’ the PDF
myDictionary = CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
// Cleanup our mess
CFRelease(myDictionary);
CFRelease(url);
// Done creating our PDF Context, now it’s time to draw to it
// Starts our first page
CGContextBeginPage (pdfContext, &pageRect);
// Draws a black rectangle around the page inset by 50 on all sides
// CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width, pageRect.size.height));
// Adding some text on top of the image we just added
//release
CGContextEndPage (pdfContext);
CGContextRelease (pdfContext);
}
这里就是创建PDF的代码。
4、在PDF上面写字或者绘图
绘图:
UIImage *image = [UIImage imageWithContentsOfFile:[saveDirectory stringByAppendingString:@"/0.png"]];
CGContextDrawImage(pdfContext, CGRectMake(623, 412, 465, 272), image.CGImage);//在坐标中画出图片
CGContextSetRGBStrokeColor(pdfContext,0,0,0,0.2);//画笔线的颜色
CGContextStrokeRect(pdfContext,CGRectMake(623, 412, 465, 272));//画方框
写字(这里我写了个方法,方便重复调用):
- (void)addText:(NSString *)text x:(CGFloat )x y:(CGFloat)y{
// Prepare font
CGFloat s = 15;
CTFontRef ctfont = CTFontCreateWithName(CFSTR("STHeitiSC-Medium"), s, NULL);
CGColorRef ctColor = [[UIColor blackColor] CGColor];
// Create an attributed string
CFStringRef keys[] = { kCTFontAttributeName,kCTForegroundColorAttributeName };
CFTypeRef values[] = { ctfont,ctColor};
CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFStringRef ctStr = CFStringCreateWithCString(nil, [text UTF8String], kCFStringEncodingUTF8);
CFAttributedStringRef attrString = CFAttributedStringCreate(NULL,ctStr, attr);
CTLineRef line = CTLineCreateWithAttributedString(attrString);
CGContextSetTextMatrix(pdfContext, CGAffineTransformIdentity);
//CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(3.14));
//CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0)); //Use this one if the view's coordinates are flipped
CGContextSetTextPosition(pdfContext, x, y);
CTLineDraw(line, pdfContext);
CFRelease(line);
CFRelease(attrString);
CFRelease(ctStr);
// Clean up
CFRelease(attr);
CFRelease(ctfont);
}
大功告成,其实这个PDF使用上下文的方式创建,然后保存为PDF而已,所以平常CG的绘图方法基本都能够用
另外就是写文字的部分,CGContextShowTextAtPoint是不能绘中文的,所以必须用CoreText的方法。
最后说一句,你们看到表格模糊是因为我偷懒,直接PS了一张png作为背景,所以看起来有点模糊,各位勤奋的同学还是自己一点一点绘比较好,也可以练习一下怎么写CG