开发者

iPad UIWebView PDF rendering is giving me weird visual artifacts

开发者 https://www.devze.com 2022-12-27 01:24 出处:网络
I am having some difficulty using a UIWebView to render PDF files on the iPad. Everything works fine i开发者_StackOverflow中文版n portrait mode, but turning the device to landscape produces strange vi

I am having some difficulty using a UIWebView to render PDF files on the iPad. Everything works fine i开发者_StackOverflow中文版n portrait mode, but turning the device to landscape produces strange visual quirkiness. Zooming in (but not out) even the slightest will correct it, but obviously that's not an ideal workaround.

The issue occurs with any PDF file (I have tried several, all stored locally in the bundle, not retrieved from the web). I also created a clone of the project for iPhone, which seems to work just fine, so the problem is iPad-specific. The problem occurs both in the simulator as well as on a physical iPad.

Screenshot

http://dev.boxkite.net/images/ipad/ipad-pdf.png

Code

 NSString* filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];
 NSData* data = [NSData dataWithContentsOfFile:filePath];

 [self.webView 
  loadData:data
  MIMEType:@"application/pdf"
  textEncodingName:@"UTF-8"
  baseURL:nil];

Thanks so much for your time.


This is indeed a known bug, according to Apple.

A better work-around is to call

-[UIView setNeedsDisplay]

on every subview in the web view instance, like so:

for(UIView* subview in webView)
{
    for(UIView *reloadView in [subview subviews])
    {
        if([reloadView class] != [UIImageView class])
        {
            [reloadView setNeedsDisplay];
        }
    }
}


The only way to fix this problem is to when ever you rotate/change the size of your UIWebView is to call loadData: again. This leaves it on for around a second (depending on file size) but is not a terrible fix for now. It is certainly much better than calling loadRequest: again!

The bug has been submitted to apple and has been approved. No word on a fix date.


It's a confirmed bug. I found a workaround that works for me. You can use this code when you resize your view or in the autorotate event.


for (UIView *v in [webView subviews]) {
  for (UIView *v2 in [v subviews]) {
    if ([v2 class] != [UIImageView class]) [v2 setNeedsDisplay];
  }
}

That's it.


I believe this is one of the many simulator bugs.


This is happening to me as well, really really infuriating. Anyone know if this problem is known to Apple? Seems like a problem with their code rather than anything we are doing wrong. The only way I found to fix it is to tell the UIWebView to reload the pdf after the orientation change - definitely not ideal (especially with large pdfs) - and even then the visual chop remains for a fraction of a second before the correct version appears.


The only way I can fix the rendering is to call reload on the UIWebView, but it's not pretty.


This is a solution without using the private sub-sub-view of the webview. It is not perfect, but a workaround without using a reload or risking your app getting rejected.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    for (UIView *v in [web subviews]) {  
        [self performSelector:@selector(correctPDFView:) withObject:v afterDelay:0.0];  
    }  
    return YES;  
}

-(void)correctPDFView:(UIScrollView*)scroll{   
    [scroll setZoomScale:scroll.minimumZoomScale animated:NO];  
    [self performSelector:@selector(correctPDFView2:) withObject:scroll afterDelay:0.5];  
}

-(void)correctPDFView2:(UIScrollView*)scroll{  
    [scroll setZoomScale:scroll.minimumZoomScale+0.01f animated:YES];  
    [self performSelector:@selector(correctPDFView3:) withObject:scroll afterDelay:0.5];  
}

-(void)correctPDFView3:(UIScrollView*)scroll{  
    [scroll setZoomScale:scroll.minimumZoomScale animated:YES];  
}
0

精彩评论

暂无评论...
验证码 换一张
取 消