在照片库中保存pdf

时间:2013-07-29 11:32:43

标签: iphone ios pdf photo-gallery

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

但我真正想要的是保存照片中的PDF 。这样用户无需打开我的应用程序即可访问它。

请提供任何帮助或建议

4 个答案:

答案 0 :(得分:2)

将PDF转换为图像并致电:

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

答案 1 :(得分: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);*/
     }

答案 2 :(得分: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);

希望这能帮到你!

答案 3 :(得分: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);
}