代码之家  ›  专栏  ›  技术社区  ›  Jamie Chapman

如何使用iOS以编程方式生成PDF的缩略图?

  •  11
  • Jamie Chapman  · 技术社区  · 15 年前

    UIWebViews 目前。理想情况下,我希望能够在 UITableView 不加载许多不同的 同时。。。他们加载一个文档已经够慢了-别介意10+!

    我该怎么做?

    UIDocumentInteractionController UIWebView

    3 回复  |  直到 6 年前
        1
  •  12
  •   Jay Bhalani    9 年前

    苹果公司在 CoreGraphics 直接绘制PDF内容的级别。据我所知,在 UIKit CGContextDrawPDFPage; 按正常值 核心图形 还有其他方法可以从数据源创建PDF引用,然后从PDF获取页面引用。然后需要处理缩放和转换到所需视图的问题,并警告您需要执行水平翻转,因为PDF(和OS X)使用左下角作为原点,而iOS使用左上角。示例代码,在我的头顶上:

    UIGraphicsBeginImageContext(thumbnailSize);
    CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithProvider( (CGDataProviderRef)instanceOfNSDataWithPDFInside );
    CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdfRef, 1); // get the first page
    
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    
    // ignore issues of transforming here; depends on exactly what you want and
    // involves a whole bunch of normal CoreGraphics stuff that is nothing to do
    // with PDFs
    CGContextDrawPDFPage(contextRef, pageRef);
    
    UIImage *imageToReturn = UIGraphicsGetImageFromCurrentImageContext();
    
    // clean up
    UIGraphicsEndImageContext();
    CGPDFDocumentRelease(pdfRef);
    
    return imageToReturn;
    

    估计你会想用 CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox)

    -renderInContext: 方法打开 CALayer (见 QA 1703 )获取屏幕截图的旧方法是 UIGetScreenImage

    不管是哪种情况,PDF都不会那么快绘制。您可能需要填充该表,然后在后台线程上绘制缩略图,在可用时将其向上推。您不能在后台线程上安全地使用UIKit对象,但是 核心图形 东西是安全的。

        2
  •  13
  •   alones    15 年前

    NSURL* pdfFileUrl = [NSURL fileURLWithPath:finalPath];
    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfFileUrl);
    CGPDFPageRef page;
    
    CGRect aRect = CGRectMake(0, 0, 70, 100); // thumbnail size
    UIGraphicsBeginImageContext(aRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIImage* thumbnailImage;
    
    
    NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf);
    
    for(int i = 0; i < totalNum; i++ ) {
    
    
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, 0.0, aRect.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
    
        CGContextSetGrayFillColor(context, 1.0, 1.0);
        CGContextFillRect(context, aRect);
    
    
        // Grab the first PDF page
        page = CGPDFDocumentGetPage(pdf, i + 1);
        CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, aRect, 0, true);
        // And apply the transform.
        CGContextConcatCTM(context, pdfTransform);
    
        CGContextDrawPDFPage(context, page);
    
        // Create the new UIImage from the context
        thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
    
        //Use thumbnailImage (e.g. drawing, saving it to a file, etc)
    
        CGContextRestoreGState(context);
    
    }
    
    
    UIGraphicsEndImageContext();    
    CGPDFDocumentRelease(pdf);
    
        3
  •  2
  •   Emil    8 年前

    下面是生成pdf页面的UIImage缩略图的swift 3方法

    static func getThumbnailForPDF(_ urlString:String, pageNumber:Int) -> UIImage? {
        let bundle = Bundle.main
        let path = bundle.path(forResource: urlString, ofType: "pdf")
        let pdfURL = URL(fileURLWithPath: path!)
    
        let pdf = CGPDFDocument(pdfURL as CFURL )!
        let page = pdf.page(at: pageNumber)!
        let rect = CGRect(x: 0, y: 0, width: 100.0, height: 70.0) //Image size here
    
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()!
    
        context.saveGState()
        context.translateBy(x: 0, y: rect.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.setFillColor(gray: 1.0, alpha: 1.0)
        context.fill(rect)
    
    
        let pdfTransform = page.getDrawingTransform(CGPDFBox.mediaBox, rect: rect, rotate: 0, preserveAspectRatio: true)
        context.concatenate(pdfTransform)
        context.drawPDFPage(page)
    
        let thumbImage = UIGraphicsGetImageFromCurrentImageContext()
        context.restoreGState()
    
        UIGraphicsEndImageContext()
    
        return thumbImage
    }