UIWebView scaling issue

Discussion in 'iOS Programming' started by Rhalliwell1, May 19, 2010.

  1. Rhalliwell1 macrumors 6502a

    Joined:
    May 22, 2008
    #1
    I have a small UIWebView which i want to scale so it shows a sort of preview like view of the web page - a bit like the top sites view in desktop safari.

    Heres my code:

    Code:
    UIWebView *webPage = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 120)];
    NSURL *pageURL = [[NSURL alloc] initWithString:@"http://www.google.com/"];
    [webPage loadRequest:[NSURLRequest requestWithURL:pageURL]];
    webPage.userInteractionEnabled = NO;
    webPage.scalesPageToFit = YES;
    
    and heres the current result:

    [​IMG]

    anyone know how to resolve this?

    Thanks :)
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Render the view (at full page size) into an image and scale that.
     
  3. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    I thought you might ask that!

    0) Reference for the functions I use is here
    1) Create a context to render into with UIGraphicsBeginImageContext
    2) Render the layer property of the view with drawInContext:.
    3) Get the image out of the context with UIGraphicsGetImageFromCurrentImageContext
    4) End the context with UIGraphicsEndImageContext

    You now have a UIImage containing the rendered view (which does not have to be on screen). You can now do what you want with it including display it at whatever scale you want in the normal way (if you don't know this read the documentation).

    I would note all of the above is discoverable if you read the documentation.
     
  5. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #5
    Thanks so much!

    I knew there was something somewhere but it is knowing where to look that is the problem :)

    thanks again!
     
  6. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #6
    The compiler is saying it cannot find the -drawInContext: method


    Code:
    UIGraphicsBeginImageContext(webPage.bounds.size);
    [webPage.layer drawInContext:UIGraphicsGetCurrentContext()]; //warning here
    UIImage *webPageImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    [​IMG]

    I tried the -renderInContext: method too but got the same result.
     
  7. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #7
    Solved it:

    Code:
    #import "QuartzCore/CALayer.h"
    :p
     

Share This Page