在iphone中显示pdf文件的缩略图

时间:2013-12-16 10:50:34

标签: ios iphone pdf uiwebview

您好我正在开发一个在UIWebView中显示pdf文件的应用程序。我想显示它包含的所有pdf文件页面的缩略图,用户可以选择他感兴趣的页面。我找到了一个示例代码来获取缩略图,但我无法显示它。

UIWebView* webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
webView.backgroundColor = [UIColor whiteColor];

NSString *fileName= @"2_slo_sample";
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"pdf"];
NSURL* pdfFileUrl = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
webView.scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 480);
webView.scrollView.alwaysBounceHorizontal = YES;

[self.view addSubview:webView];

CGPDFPageRef page;
CGRect aRect = CGRectMake(0, 0, 70, 100); // thumbnail size
UIGraphicsBeginImageContext(aRect.size);

CGContextRef context = UIGraphicsGetCurrentContext();
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfFileUrl);
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, kCGPDFCropBox, aRect, 0, false);
    // And apply the transform.
    CGContextConcatCTM(context, pdfTransform);

    CGContextDrawPDFPage(context, page);

    // Create the new UIImage from the context
    thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();

    CGContextRestoreGState(context);
}

UIGraphicsEndImageContext();
CGPDFDocumentRelease(pdf);

我确定我正在掩饰其中的一些错误。你能否告诉我需要纠正的地方?

1 个答案:

答案 0 :(得分:0)

就在for循环之上

int x, y, w, h;
x = 5;
y = 5;

现在:

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, kCGPDFCropBox, aRect, 0, false);
// And apply the transform.
CGContextConcatCTM(context, pdfTransform);

CGContextDrawPDFPage(context, page);

// Create the new UIImage from the context
thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
**[self drawImage:thumbnailImage withX:x withY:y];**
**x = x +100;**
CGContextRestoreGState(context);

}

和方法定义为:

-(void)drawImage:(UIImage *)image withX:(int)x withY:(int)y

{

UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame =CGRectMake(x, y, **image.size.width**, **image.size.height**);
imageView.image = image;
[self.webView addSubview:imageView];

}

理想情况下,您应该在视图中添加pdf缩略图/而不是在Web视图中添加(如果需求则可以) 希望这会对你有所帮助。

相关问题