【问题标题】:Display thumbnails for pdf files in iphone在 iphone 中显示 pdf 文件的缩略图
【发布时间】: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 中显示

标签: ios iphone pdf uiwebview


【解决方案1】:

就在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];

}

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

【讨论】:

  • 感谢您的关注,但我有一个小问题,它一次只显示一个图像,而且也不太清楚......
  • 是的,除了在 [self.webview addSubview:imageView] 中我做了同样的事情,我遇到了一些错误,所以我用 [self->webView addSubview:imageView] 替换了它;
  • 检查我的编辑,我们不应该将宽度和高度强行设置为 400。所以我们可以利用 UIImage 属性的宽度和高度(你得到的缩略图有它的宽度和高度,这将使你图像看起来很清晰)。也让你的 webView 对象全局变量,只需使用 [webView addSubview:imageView];
  • 你应该添加一个水平滚动视图作为 web 视图的子视图,并在滚动视图中添加 uiimage 视图,如果将来你会说 100 个缩略图,你将能够查看所有图像 y滚动
  • 感谢老兄,它的工作 fyn 现在感谢您的帮助,非常感谢您
猜你喜欢
  • 2012-11-19
  • 2011-02-21
  • 1970-01-01
  • 2011-08-05
  • 2018-01-09
  • 2017-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多