iphone - 在 iphone 中创建 pdf 缩略图

标签 iphone objective-c pdf

我是 Objective-c iPhone 编程的新手。我有一个应用程序可以在我的 UIWebView 中成功显示 PDF,但现在我想创建我的 PDF 的缩略图。我的 PDF 存储在我的资源文件夹中。

因此,请提供有关如何显示 PDF 缩略图的代码。我的代码用于显示按钮功能中获取的 PDF:

-(void)show:(id)sender {

    pdfView.autoresizesSubviews = NO;
    pdfView.scalesPageToFit=YES;
    pdfView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    [pdfView setDelegate:self];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"com" ofType:@"pdf"];
    NSLog(@"Path of res is%@",path);
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [pdfView loadRequest:request];
}

最佳答案

试试下面的方法:

- (UIImage *)imageFromPDFWithDocumentRef:(CGPDFDocumentRef)documentRef {
    CGPDFPageRef pageRef = CGPDFDocumentGetPage(documentRef, 1);
    CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);

    UIGraphicsBeginImageContext(pageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, CGRectGetMinX(pageRect),CGRectGetMaxY(pageRect));
    CGContextScaleCTM(context, 1, -1);  
    CGContextTranslateCTM(context, -(pageRect.origin.x), -(pageRect.origin.y));
    CGContextDrawPDFPage(context, pageRef);

    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return finalImage;
} 

关于iphone - 在 iphone 中创建 pdf 缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5658993/

相关文章:

android - 从 Android 应用程序创建输出文档

c++ - 如何为 Objective C 编写 C++ 处理类

iphone - Objective-C 中的音频缓冲区修改

objective-c - 加载 .xib 时出错 - 应用程序崩溃

android - 保存 pdf 而不向用户显示打印对话屏幕

PHP、代码点火器、Dompdf : HTML generating fine but exectuion time out whlie rederning PDF

ios - 当 segmentedControl 改变时在单元格之间切换

iphone - 将动态输入的数据保存到 plist

ios - UIView 未出现在 UIViewController 上

ios - 在导航栏和 UITableView 之间插入边距