作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
iOS SDK 中是否有内置机制,我可以使用它来呈现任意文档类型的缩略图 (UIImage),例如Word、Excel、PDF、图像格式 等 - 任何 iOS 都可以预览?它只需要创建第一页的缩略图。
我有缩小 PDF 和图像的代码,但我想,由于 iOS 可以预览 Word 和其他软件,也许有内置的东西?
最佳答案
使用技巧打开办公文档文件(docx、xls、pptx),在缩略图栏中显示所有页面,让用户在点击任何缩略图的同时打开同一 View 中的任何页面。
步骤如下
导入QuartzCore.framework
Confugure.h 文件类似
#import <UIKit/UIKit.h>
#include <QuartzCore/QuartzCore.h>
@interface ViewController : UIViewController<UIWebViewDelegate>
{
BOOL finished;
float currentOffset;
float pointToAddNextThumbnail;
NSURLRequest *request;
int totalFileHeight;
int currentExecutingSnapshot;
}
@property(nonatomic,retain) UIWebView* webView;
@property(nonatomic,retain) UIScrollView* thumbnailScrollView;
@end
.m 类文件
#import "ViewController.h"
@interface ViewController ()
@end
#define pageHeight 360
#define pageWidth 320
#define thumbnailHeight 88
#define thumbnailWidht 70
@implementation ViewController
@synthesize webView,thumbnailScrollView;
#pragma mark
#pragma mark VC Delegates
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *path = [[NSBundle mainBundle] pathForResource:@"Reader" ofType:@"doc"];
NSURL *url = [NSURL fileURLWithPath:path];
request = [NSURLRequest requestWithURL:url];
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, pageWidth, pageHeight)];
[self.webView setScalesPageToFit:YES];
self.webView.delegate = self;
[self.webView loadRequest:request];
[self.view addSubview:self.webView ];
self.thumbnailScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, pageHeight+5, pageWidth, thumbnailHeight)];
[self.thumbnailScrollView setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:self.thumbnailScrollView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark
#pragma mark webView Delegate Methods
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// NSLog(@"webViewDidFinishLoad");
CGSize z = self.webView.scrollView.contentSize;
totalFileHeight= z.height;
NSLog(@"totalFileHeight in pxls %d",totalFileHeight);
totalFileHeight=totalFileHeight/pageHeight;
NSLog(@"totalFileHeight in pages %d",totalFileHeight);
[self takeScreenShotAndReloadWebViewWithPage];
}
#pragma mark
#pragma mark utitlityMehtods
-(void)takeScreenShotAndReloadWebViewWithPage
{
float widthOfThumbnailForScrollView = (thumbnailWidht+5);
float widhtOfScrollViewContant = (currentExecutingSnapshot*widthOfThumbnailForScrollView)+widthOfThumbnailForScrollView;
self.thumbnailScrollView.contentSize = CGSizeMake(widhtOfScrollViewContant,50);
self.webView.scrollView.contentOffset = CGPointMake(0, currentOffset);
// [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0,%f)",currentOffset]];
//take Snapshot
UIGraphicsBeginImageContext(CGSizeMake(320, pageHeight));
[self.webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//set frames of thumbailButton and thumnailImage
CGRect thumbnailFrame = CGRectMake(pointToAddNextThumbnail, 0, thumbnailWidht, thumbnailHeight);
UIButton *thumbnailButton = [[UIButton alloc] initWithFrame:thumbnailFrame];
UIImageView *thumbnailImage =[[UIImageView alloc] initWithFrame:thumbnailFrame];
[thumbnailImage setImage:img];
thumbnailButton.tag = currentOffset;
[thumbnailButton setBackgroundColor:[UIColor clearColor]];
[thumbnailButton addTarget:self action:@selector(thumnailButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.thumbnailScrollView addSubview:thumbnailImage];
[self.thumbnailScrollView addSubview:thumbnailButton];
[thumbnailImage release];
[thumbnailButton release];
pointToAddNextThumbnail = pointToAddNextThumbnail+thumbnailWidht+5;
currentOffset = currentOffset + pageHeight;
if (currentExecutingSnapshot < totalFileHeight)
{
NSLog(@"currentExecutingSnapshot %d",currentExecutingSnapshot);
currentExecutingSnapshot ++;
//[self.webView loadRequest:request];
//make a recursive call and take snapshot of all pages
[self takeScreenShotAndReloadWebViewWithPage];
}
else
{
[self finishedFethingThumbnails];
}
}
-(void)finishedFethingThumbnails
{
self.webView.scrollView.contentOffset = CGPointMake(0, 0);
}
#pragma mark
#pragma mark IBaction
-(IBAction)thumnailButtonClicked:(UIButton*)sender
{
[[self.webView scrollView] setZoomScale:0 animated:YES];
[[self.webView scrollView] setContentOffset:CGPointMake(0, sender.tag) animated:YES];
}
#pragma mark
#pragma mark dealloc
-(void)dealloc
{
[webView release];
[thumbnailScrollView release];
[super dealloc];
}
@end
关于ios - 有没有一种简单的方法可以在 iOS 上创建各种文档格式(如 Word、Excel)的缩略图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8143886/
我有一个具有可变数量子元素的固定大小的 div。我不知道 children 的大小。目标是缩小它们以适合父级。 例子: .parent { width: 100px; height: 100p
我是一名优秀的程序员,十分优秀!