Question

I'd like to add a Web View to my app at 60% scale (like seen in Safari in the browse other windows view):

alt text

Notice how the content looks nice and Aliased!

If I try and add the same Web view to my app:

NSURL *url = [NSURL URLWithString:@"http://www.google.co.uk?q=hello"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
webView.delegate=self;
[webView loadRequest:request];  
[self.view addSubview:webView];

Using the following transformation:

[webView setTransform:CGAffineTransformMakeScale(0.6, 0.6)];

alt text

..the scale is really bad quality and there appears to be no anti-aliasing.

Does anyone know why this is happening or have a suggestion on how it could be fixed?

Thanks! Nick.

Was it helpful?

Solution

I figured out a solution!

Take a snapshot of the webview, add it to a UIImageView and resize that insted!

UIGraphicsBeginImageContext(webView.bounds.size);
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(webView.bounds.x, webView.bounds.y, webView.frame.size.width, webView.size.height)];
imageView.image = viewImage;
[self addSubview:imageView];
[imageView release];

webView.hidden = YES;

Seems to work fine!

One point to note.. if you want to do this as soon as the WebView has loaded, you need to defer the action by some period (I chose a 10th of a second!)

Like so:

-(void)deferLoading
{
    webViewHasActuallyLoaded = YES;
    [self setNeedsLayout];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [NSTimer scheduledTimerWithTimeInterval: 1.0/10.0 target:self selector:@selector(deferLoading) userInfo:nil repeats:NO];
}

Cheers for suggestions abovee! Nick.

OTHER TIPS

Why not just make the web view smaller? It should render the content in high quality at the smaller size.

If you need to animate the change in size, you can set the minificationFilter on the web view's layer to get reasonable quality:

[[webView layer] setMinificationFilter:kCAFilterLinear];

In your UIViewController override

  • (void)placeViewItems: (UIInterfaceOrientation)interfaceOrientation

And make your UIWebView's frame.origin.x & frame.origin.y integer values. See if that is the result you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top