简体   繁体   English

将pdf保存到照片库

[英]Save pdf in Photo Gallery

I have the pdf file in the Document Directory. 我在文档目录中有pdf文件。 I can access it through code easily and show it to user. 我可以轻松地通过代码访问它并将其显示给用户。

But What I exactly want is to SAVE THE PDF IN PHOTOGALLERY . 但是我真正想要的是将PDF保存在照相馆中 So that user can access it without opening my application. 这样,用户无需打开我的应用程序即可访问它。

Any help or suggestions please 任何帮助或建议请

Convert your PDF into an image and call: 将您的PDF转换为图像并调用:

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

Try something like this: 尝试这样的事情:

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

Convert an pdf to image :- 将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 ];

Now save it in Photo gallery :- 现在将其保存在照片库中:-

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

Hope this helps you ! 希望这对您有帮助!

In order to manage number of pages you need to do this: 为了管理页面数,您需要执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM