0

我正在尝试使用 xcode 4.6 从 iPad 应用程序创建 PDF 报告。我知道在模拟器上运行时正在创建一个有效的 pdf 文件,因为我可以将其挖掘出来并进行预览。注释掉的代码就是这样做的。问题是我不能把它写在我可以在 iPad 上找到的地方。

我尝试改用 UIGraphicsBeginPDFContextToData 并尝试将图像写到 PhotoAlbum 中。这里的问题是,当我将 NSMutableData 转换为图像时,它返回 nil。

这是代码。感谢你给与我的帮助。

- (IBAction)makePDF:(UIButton *)sender
{
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (CFStringRef)self.labelCopyright.text, NULL);

    if (currentText)
    {
        CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
        if (framesetter)
        {
//            NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, //NSUserDomainMask, YES) objectAtIndex:0];
//            NSString *pdfPath = [rootPath stringByAppendingPathComponent:@"Nick.pdf"];
//            NSLog(@"pdf is at %@",pdfPath);
//            UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil);
            NSMutableData *data = [[NSMutableData alloc] initWithCapacity:100000];
            UIGraphicsBeginPDFContextToData(data, CGRectZero, nil);
            CFRange currentRange = CFRangeMake(0, 0);
            NSInteger currentPage = 0;
            BOOL done = NO;

            do
            {
                UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
                currentPage++;
//                [self drawPageNumber:currentPage];

                currentRange = [self renderPage:currentPage withTextRange:currentRange andFramesetter:framesetter];               
                if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)currentText)) done = YES;
            }
            while (!done);
            UIGraphicsEndPDFContext();
            UIImage* image = [UIImage imageWithData:data];
            assert(image);
            UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
            CFRelease(framesetter);
        }
        else NSLog(@"Could not create the framesetter needed to lay out the atrributed string.");
        CFRelease(currentText);
    }
    else NSLog(@"Could not create the attributed string for the framesetter");
}

- (CFRange)renderPage:(NSInteger)pageNum withTextRange:(CFRange)currentRange andFramesetter:(CTFramesetterRef)framesetter
{
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
    CGRect frameRect = CGRectMake(72, 72, 468, 648);
    CGMutablePathRef framePath = CGPathCreateMutable();

    CGPathAddRect(framePath, NULL, frameRect);
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);

    CGPathRelease(framePath);
    CGContextTranslateCTM(currentContext, 0, 792);
    CGContextScaleCTM(currentContext, 1.0, -1.0);
    CTFrameDraw(frameRef, currentContext);
    currentRange = CTFrameGetVisibleStringRange(frameRef);
    currentRange.location += currentRange.length;
    currentRange.length = 0;
    CFRelease(frameRef);

    return currentRange;
}
4

2 回答 2

0

不要将 PDFNSMutableData写入对象,而是使用UIGraphicsBeginPDFContextToFile.

第一个参数是文件路径。最好的地方是Documents目录。然后有许多不同的方法可以将文件从应用程序中取出:

  1. iTunes 文件共享
  2. 电子邮件
  3. iCloud
  4. 发送到第三方服务器(Dropbox、Box、Google Drive 等)
  5. 使用 .在另一个 iOS 应用程序中打开UIDocumentInteractionController
于 2013-02-17T05:42:08.303 回答
0

将可变数据保存到您的文档目录

[data writeToFile:filePath atomically:YES]

这是一个例子:

+(void) saveData: (NSData*) data ToFileName: (NSString*) filename {

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent: filename];

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

至于在设备上显示生成的 PDF,该UIWebView对象支持从NSData. 这是一个例子:

[self.webView loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];

也可以将NSData对象附加到电子邮件。这是一个例子:

//Check if we can send e-mails
if ([MFMailComposeViewController canSendMail]) {

    //Create the Email view controller
    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;

    //Set the subject and body
    [controller setSubject:@"Email Subject"];
    [controller setMessageBody:@"Email body" isHTML:NO];

    //Set the email address
    [controller setToRecipients:@"test@test.com"];

    //Add the current PDF as an attachment
    NSString *fileName = @"file.pdf";

    [controller addAttachmentData:self.retrievedPDF mimeType:@"application/pdf" fileName:fileName];

    // show the email controller modally
    [self.navigationController presentModalViewController: controller animated: YES];

}
于 2013-02-17T05:48:06.263 回答