【发布时间】:2013-12-16 10:50:34
【问题描述】:
您好,我正在开发一个在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);
我确定我掩盖了其中的一些错误。你能告诉我哪里需要改正吗?
【问题讨论】:
-
你如何/在哪里设置 thumbnailImage 在 web 视图中??
-
我无法在网页视图中设置缩略图...这是我面临的问题,您能指导我设置缩略图吗..
-
你为什么要在网页视图中设置缩略图,我想你想在网页视图中设置原始图像(缩略图的源图像)。
-
实际上我的要求是在 webview 中显示 pdf 文件,与报刊亭中显示的 pdf 文件相同,用户可以在水平滚动中预览所有 pdf 页面,他可以选择自己的。以同样的方式我需要在 UIWebview 中显示