缩放/捏合在UIWebView中加载的pdf

时间:2013-07-03 10:31:26

标签: ios pdf uiwebview resourcebundle pinchzoom

我已从资源包中的UIWebView中加载PDF文件。

现在,我无法缩放/捏合PDF文件。

我找到了以下2个相关链接,但没有一个答案被标记为正确 -

  1. How to zoom webView with pdf file

  2. Enable zooming/pinch on UIWebView

  3. 如何使资源包中的UIWebView中加载的PDF可缩放/可缩放,以下解决方案是否有效?

    1. Problem with pdf while opening in UIWebView
    2. 感谢您的帮助。

3 个答案:

答案 0 :(得分:8)

界面生成器上的WebView中,添加检查缩放页面以适合,然后启用Pinch ZoomIn ZoomOut;)

或者,如果您想更好地使用PDF,请尝试查看此代码:

- (void)viewDidLoad 
{
    [super viewDidLoad];

    [webView loadRequest:[NSURLRequest requestWithURL:@"http:pdfURL"]];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"yourPDFFile" ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    //--------------AND HERE USE SCALE PAGE TO FIT------------------//
    [webView setScalesPageToFit:YES];

}

UIWebView 希望这能帮助你。

答案 1 :(得分:3)

//试试这个

NSString *urlstr=@"www.example.com/yy.pdf";
web=nil;
web=[[UIWebView alloc] initWithFrame:CGRectMake(0, 98, 320, 367)];
web.delegate=self;
[self.view addSubview:web];
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlstr]]];
UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc]
                                 initWithTarget:self action:@selector(handlePinch:)];
 pgr.delegate = self;
 [web addGestureRecognizer:pgr];

//目标行动

 - (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
 {
  recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
  }

在此之前在.h中添加UIGestureRecognizerDelegate。希望它会帮助你..

答案 2 :(得分:1)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:documentsDirectory]) 
{
    NSURL *url = [NSURL fileURLWithPath:strFileName];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView setScalesPageToFit:YES];
    [webview loadRequest:request];
}