【问题标题】:Writing multiple UIWebViews as UIImages to PDF file将多个 UIWebViews 作为 UIImages 写入 PDF 文件
【发布时间】:2017-03-18 20:10:18
【问题描述】:

我正在使用图表创建库 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 时,我得到了这个:

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

我需要两个图表同时并排显示。我计划在下面再添加两个图表,也是并排的。有人可以提供有关如何解决此问题的建议/代码吗?我相信问题出在我的上下文中。我只是不确定如何制作,以便在打印第一个图表时不会省略第二个图表。

【问题讨论】:

  • 你能澄清一下“解决这个问题”在这种情况下的含义吗?您是否关心红线的垂直定位?认知图表与其标签的重叠?第一张图片中缺少的认知测试?
  • 很抱歉不清楚。我希望两个图表同时出现。想象第一张图片,但第二张图片中的图表也在 PDF 上。我最终还将在这些图表下方添加两个图表。我认为这与我的图像上下文有关,但不确定。
  • 方法-addImage:atPoint:从何而来?这是您自己的,还是在 PDF 或图表处理库中?

标签: ios objective-c uiwebview uiimage


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    相关资源
    最近更新 更多