1

我在文档目录中有 pdf 文件。我可以通过代码轻松访问它并将其展示给用户。

但我真正想要的是将PDF 保存在 PHOTOGALLERY中。这样用户就可以在不打开我的应用程序的情况下访问它。

请有任何帮助或建议

4

4 回答 4

2

Convert your PDF into an image and call:

UIImageWriteToSavedPhotosAlbum(UIImage *image, 
                                    id completionTarget, 
                                   SEL completionSelector, 
                                  void *contextInfo);
于 2013-07-29T11:37:08.053 回答
0

尝试这样的事情:

    NSString *path = [FindThePathOfPDF stringByAppendingPathComponent:@"YOURPDFFILENAME"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Check if the file exists
    if ([fileManager fileExistsAtPath:path])
    {

    CFURLRef pdfURL = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)path, kCFURLPOSIXPathStyle, FALSE);

    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(pdfURL);

    CFRelease(pdfURL);

    CGPDFPageRef page;

    CGContextRef context = UIGraphicsGetCurrentContext();
    UIImage* imageFromPDF;

    //pass any page number you want
    page = CGPDFDocumentGetPage(pdf,1);

    CGContextDrawPDFPage(context, page);

    imageFromPDF = UIGraphicsGetImageFromCurrentImageContext();

    CGContextRestoreGState(context);
    UIGraphicsEndImageContext();
    CGPDFDocumentRelease(pdf);
    CFRelease(pdfURL);

    //pass imageFromPDF to the below function and get it done. 
    /*UIImageWriteToSavedPhotosAlbum(UIImage *image, 
                                id completionTarget, 
                               SEL completionSelector, 
                              void *contextInfo);*/
     }
于 2013-07-29T11:48:57.020 回答
0

将pdf转换为图像:-

  CGPDFDocumentRef PDFfile;

        CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("iPhone Sample apps.pdf"), NULL, NULL);
        PDFfile = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
        CFRelease(pdfURL);

 CGPDFPageRef page = CGPDFDocumentGetPage(PDFfile,currentpage);

    context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextFillRect(context,self.bounds);   
    CGContextTranslateCTM(context, -1.0, [self bounds].size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page,  kCGPDFArtBox, [self bounds], 0, true));
    CGContextDrawPDFPage(context, page);    
    CGContextRestoreGState(context);
   CGImageRef imgref;
    imgref=CGBitmapContextCreateImage(context);      
    image= [[UIImage alloc]initWithCGImage:imgref ];

现在将其保存在照片库中:-

  UIImageWriteToSavedPhotosAlbum(UIImage *image, 
                                id completionTarget, 
                               SEL completionSelector, 
                              void *contextInfo);

希望这对你有帮助!

于 2013-07-29T11:51:30.083 回答
0

为了管理页数,您需要执行以下操作:

CGPDFDocumentRef tempDocRef = CGPDFDocumentCreateWithURL((CFURLRef)tempPdfUrl);
size_t pageCount = CGPDFDocumentGetNumberOfPages(docRef);
CGContextRef finalPdfContext = CGPDFContextCreateWithURL((CFURLRef)tempPdfUrl, &defaultPageRect, nil);

for (int pageNum = 1; pageNum <= pageCount; pageNum++) {
  CGPDFPageRef tempPageRef = CGPDFDocumentGetPage(tempDocRef, pageNum+1);
  CGPDFContextBeginPage(finalPdfContext, nil);
  CGContextDrawPDFPage(finalPdfContext, tempPageRef);

    UIImage *newimage =  UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(newimage, self, nil, nil);


  CGPDFContextEndPage(finalPdfContext);
  CGPDFPageRelease(tempPageRef);
}
于 2013-07-29T11:57:55.773 回答