簡體   English   中英

在iPhone中顯示pdf文件的縮略圖

[英]Display thumbnails for pdf files in iphone

您好,我正在開發一個在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);

我確定我掩蓋了其中的一些錯誤。 你能告訴我我需要糾正的地方嗎?

就在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縮略圖/不在網絡視圖中添加pdf縮略圖(如果需要的話,可以)希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM