IOS开发中怎样生成PDF文件和绘制渐变?

一、生成PDF文件步骤:
1创建PDF上下文
//1>.获取沙盒路径
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
//2>.拼接路径
NSString *PDFPath = [path stringByAppendingPathComponent:@"123.pdf"];
//3>.创建PDF上下文
UIGraphicsBeginPDFContextToFile(PDFPath, CGRectZero, NULL);
2.创建PDF页面
1.初始化image,画image;
for (int i = 0; i < 6; i++) {
if (i % 2 == 0) {
//创建PDF页
UIGraphicsBeginPDFPage();
}
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"NatGeo%02d",i+1]];
[image drawInRect:CGRectMake(0, (i % 2) * h, w, h)];
}
2.1添加文字水印
// 添加文字
NSString *text = @"www.baidu.com";

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentCenter;

NSDictionary *dict = @{NSFontAttributeName: [UIFont systemFontOfSize:18],
NSParagraphStyleAttributeName: paragraph,
NSForegroundColorAttributeName: [UIColor whiteColor]};

[text drawInRect:CGRectMake(0, (i % 2) * h + h - 40, w, 40) withAttributes:dict];
3.关闭上下文
UIGraphicsEndPDFContext();
二、渐变
1>线性渐变
//1.获得上下文
CGContextRef context = UIGraphicsGetCurrentContext();

//绘制渐性渐变
//创建色彩空间
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
//颜色组件,每四个一组
CGFloat components[] = {1.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0};
//开始和结束的位置数组,以百分比来表示的
CGFloat location[] = {0.0,1.0};
//创建渐变
CGGradientRef gradinet = CGGradientCreateWithColorComponents(space,components, location, 2);
//裁剪
// CGContextClipToRect(context, CGRectMake(0, 20, 100, 100));
//开始绘制
CGContextDrawLinearGradient(context, gradinet, CGPointMake(0, 0), CGPointMake(320, 460), kCGGradientDrawsAfterEndLocation);
//释放内容
CGGradientRelease(gradinet);
CGColorSpaceRelease(space);
2>绘制径向渐变
CGContextDrawRadialGradient(context, gradinet, CGPointMake(200, 240), 80, CGPointMake(160, 300), 200, kCGGradientDrawsBeforeStartLocation);
其他的步骤和线性渐变都一样。

原文地址:https://www.cnblogs.com/yinqiang/p/3486799.html