首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iOS6.1中存储为iPad‘动态’生成的PDF

在iOS6.1中存储为iPad‘动态’生成的PDF
EN

Stack Overflow用户
提问于 2013-02-16 07:03:32
回答 2查看 297关注 0票数 0

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

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

下面是代码。谢谢你能给我的任何帮助。

代码语言:javascript
复制
- (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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-17 13:48:06

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

[data writeToFile:filePath atomically:YES]

下面是一个例子:

代码语言:javascript
复制
+(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加载PDF文件。下面是一个示例:

代码语言:javascript
复制
[self.webView loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];

下面是一个示例:

代码语言:javascript
复制
//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];

}
票数 0
EN

Stack Overflow用户

发布于 2013-02-17 13:42:08

使用UIGraphicsBeginPDFContextToFile将其写入文件,而不是将PDF写入NSMutableData对象。

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

通过sharing

  • Email

  • iCloud

  • Sending连接到第三方服务器(Dropbox、盒子、谷歌硬盘等)的
  1. iTunes文件
  2. 使用UIDocumentInteractionController.

在另一个iOS应用程序中打开

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14904844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档