将多个UIWebViews作为UIImages写入PDF文件

时间:2017-03-18 20:10:18

标签: ios objective-c uiwebview uiimage

我正在使用图表创建库TWRCharts,它是UIWebView的子类,用于将多个图表绘制到PDF文件中,而且我在向PDF文件中添加多个图表时遇到了问题。

我使用以下代码调用我的图表创建方法:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createTapImageFromChart];
    [self createCogImageFromChart];
}

这些是被称为(部分)的方法:

- (void)createTapImageFromChart {

    // Chart View
    _chartViewTap = [[TWRChartView alloc] initWithFrame:CGRectMake(0, 0, 400, 300)];
    _chartViewTap.backgroundColor = [UIColor clearColor];

    // Hide the chartview until needed
    _chartViewTap.hidden = YES;

    // Add the chart view to the controller's view
    [self.view addSubview:_chartViewTap];
}

- (void)createCogImageFromChart {

    // Chart View
    _chartViewCog = [[TWRChartView alloc] initWithFrame:CGRectMake(0, 0, 400, 300)];
    _chartViewCog.backgroundColor = [UIColor clearColor];

    // Hide the chartview until needed
    _chartViewCog.hidden = YES;

    // Add the chart view to the controller's view
    [self.view addSubview:_chartViewCog];

}

然后我用来捕获图表并将它们绘制到PDF文件的代码是:

//////////// Capture Tap Chart ///////////////

_chartViewTap.hidden = NO;
UIGraphicsBeginImageContext(_chartViewTap.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
[_chartViewTap.layer renderInContext:context];
UIGraphicsPopContext();
self.chartImageTap = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_chartViewTap.hidden = YES;

///////////// End Capture Tap Chart //////////

UIImage *anImage = self.chartImageTap;
CGRect tapHeader = [self addText:@"Finger Tap Test"
                            withFrame:CGRectMake(kPadding, blueLineRect.origin.y + blueLineRect.size.height + kPadding, _pageSize.width - kPadding*2, 4) fontSize:18.0f];
CGRect imageRect = [self addImage:anImage
                          atPoint:CGPointMake(/*(_pageSize.width/2)-(anImage.size.width/2)*/ kPadding, tapHeader.origin.y + tapHeader.size.height + kPadding)];

//////////// Capture Cog Chart ///////////////

_chartViewCog.hidden = NO;
UIGraphicsBeginImageContext(_chartViewCog.bounds.size);
CGContextRef contextCog = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(contextCog);
[_chartViewCog.layer renderInContext:contextCog];
UIGraphicsPopContext();
self.chartImageCog = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_chartViewCog.hidden = YES;

///////////// End Capture Cog Chart //////////

UIImage *anImageCog = self.chartImageCog;
CGRect cogHeader = [self addText:@"Cognitive Test"
                       withFrame:CGRectMake(420, blueLineRect.origin.y + blueLineRect.size.height + kPadding, _pageSize.width - kPadding*2, 4) fontSize:18.0f];
CGRect imageRectCog = [self addImage:anImageCog
                          atPoint:CGPointMake(420, blueLineRect.origin.y + blueLineRect.size.height + kPadding)];

当我运行并保存PDF时,我明白了:

enter image description here

但是当我注释掉[self createTapImageFromChart];和“捕获分析图表”部分,我在PDF中得到了这个:

enter image description here

我需要两个图表同时并排显示。我打算在下面再添加两个图表,也是并排的。有些人可以提供建议/代码来解决这个问题吗?我认为问题出在我的背景下。我只是不确定如何制作它,以便在打印第一张图表时不会省略第二张图表。

1 个答案:

答案 0 :(得分:0)

尝试添加修改作为子视图添加的图表视图的位置。实施例

_chartViewTap = [[TWRChartView alloc] initWithFrame:CGRectMake(0, 0, 400, 300)];
_chartViewCog = [[TWRChartView alloc] initWithFrame:CGRectMake(410, 0, 400, 300)];