【问题标题】:Save a UIView that contains a UILabel,UIImageView and UITableView as PDF in Objective C在 Objective C 中将包含 UILabel、UIImageView 和 UITableView 的 UIView 保存为 PDF
【发布时间】:2016-10-06 11:32:31
【问题描述】:

我的 iOS 应用程序将选定的联系人详细信息保存为 PDF 并在 WebView 中显示。
我的视图显示选定的联系人图像(在 UIImage 中)、姓名(在 UILabel 中)和电话号码等其他详细信息(在 UITableView 中) .
现在,我需要将此视图另存为 PDF 并在另一个视图中查看。
我现在使用的方法createPDFfromUIView 将我的视图保存为PDF。

但是我在我的 PDF 中看不到整个表格视图。
我需要滚动才能看到整个表格视图。

我该如何解决这个问题?

-(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();

    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
    [aView.layer renderInContext:UIGraphicsGetCurrentContext()];

    // 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);
}

【问题讨论】:

  • @New16 这仅包含一个 tableView。我还需要在另存为 pdf 之前合并一个标签和一个图像视图。我该怎么做?
  • 取主 UIViewlayer 属性,它们是它们的一部分。

标签: ios objective-c uitableview pdf


【解决方案1】:

成功了!

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename

{ CGRectpriorBounds = self.contactDetails.bounds; CGSizefittedSize = [self.contactDetails sizeThatFits:CGSizeMake(priorBounds.size.width, self.contactDetails.contentSize.height)]; self.contactDetails.bounds = CGRectMake(0, 0, compatibleSize.width, compatibleSize.height);

CGRect pdfPageBounds = CGRectMake(0, 30, 312, 800); // Change this as your need
NSMutableData *pdfData = [[NSMutableData alloc] init]; // Creates a mutable data object for updating with binary data, like a byte array

CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [UIColor redColor].CGColor);
CGContextFillRect(UIGraphicsGetCurrentContext(), _detailView.bounds);

UIGraphicsBeginPDFContextToData(pdfData, _detailView.bounds, nil);
{

    int page = 1;
    for (CGFloat pageOriginY = 0; pageOriginY < fittedSize.height; pageOriginY += pdfPageBounds.size.height)
    {

        if(page == 1)
        {
            UIGraphicsBeginPDFPageWithInfo(_detailView.bounds, nil);

            CGContextSaveGState(UIGraphicsGetCurrentContext());
            {


                [_contactImage.image drawInRect:CGRectMake( 200, 10, _contactImage.bounds.size.width, _contactImage.bounds.size.height)];
                NSDictionary *attributes = @{
                                             NSFontAttributeName   : [UIFont fontWithName:@"Arial" size:20],
                                             NSStrokeWidthAttributeName    : @(0),
                                             NSStrokeColorAttributeName    : [UIColor blackColor]
                                             };
                [_contactName.text drawAtPoint:CGPointMake(30 , 50) withAttributes:attributes];


                CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, 130);
                [self.contactDetails.layer renderInContext:UIGraphicsGetCurrentContext()];


            } CGContextRestoreGState(UIGraphicsGetCurrentContext());

        }
        else
        {
            UIGraphicsBeginPDFPageWithInfo(_detailView.bounds, nil);
            CGContextSaveGState(UIGraphicsGetCurrentContext());
            {
                CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY);
                [self.contactDetails.layer renderInContext:UIGraphicsGetCurrentContext()];

            } CGContextRestoreGState(UIGraphicsGetCurrentContext());
        }
        page++;
    }

} UIGraphicsEndPDFContext();

self.contactDetails.bounds = priorBounds; // Reset the tableView


// Use the pdfData to
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString* filePathPDF = [documentsPath stringByAppendingPathComponent:@"myPdf.pdf"]; //Add the file name
BOOL written = [pdfData writeToFile:filePathPDF atomically:YES];

}

【讨论】:

    猜你喜欢
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多