0

我正在使用下面的代码PDFUIView已经有子视图的创建。我正在获取所有子视图并在 pdf 上下文中重绘。但我无法改造UIImageView。下面是我的代码

NSMutableData *pdfData = [NSMutableData data];
CGRect pageFrame = CGRectMake(0, 0, aView.frame.size.width, aView.frame.size.height);
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);
if([view isKindOfClass:[UIImageView class]]) {
    UIImageView *imgv = (UIImageView *)view;
    [imgv.image  drawInRect:CGRectMake(view.frame.origin.x + imgv.frame.origin.x, view.frame.origin.y + imgv.frame.origin.y, imgv.frame.size.width, imgv.frame.size.height)];
}
UIGraphicsEndPDFContext();

即使图像在我的UIView但 pdf 中旋转,但没有显示旋转。如何在pdf上下文中将其转换为 90 度?

4

2 回答 2

0

请注意,以下方法仅创建视图的位图;它不会创建实际的排版。

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [aView.layer renderInContext:pdfContext];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}

还要确保导入:QuartzCore/QuartzCore。

于 2018-04-24T10:52:44.693 回答
0

如果您只需要绘制一些带有旋转的元素,请尝试对当前上下文使用矩阵变换函数(它将是 PDF 上下文)。

CGContextRotateCTM(currentContext, M_PI / 2);
于 2018-04-24T11:05:44.177 回答