Get thumbnail of page?

Discussion in 'iOS Programming' started by ArtOfWarfare, Apr 12, 2013.

  1. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    I have this code to take a URL and get me a thumbnail of the page, but none of the delegate methods for UIWebView are being called:

    Code:
    CGFloat width = [[UIScreen mainScreen] applicationFrame].size.width;
    CGFloat height = [[UIScreen mainScreen] applicationFrame].size.height;
    CGFloat max = width > height?width:height;
    UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, max, max)];
    webView.delegate = self;
    NSLog(@"Load request: %@", @"http://www.macrumors.com");
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.macrumors.com"]]];
    It is printing out the log message above. It never prints out a log for either of these:

    Code:
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        NSLog(@"Load request finished");
        UIGraphicsBeginImageContext(webView.bounds.size);
        [webView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *webViewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        NSData *imageData = UIImagePNGRepresentation(webViewImage);
        Page *newPage = [NSEntityDescription insertNewObjectForEntityForName:@"Page" inManagedObjectContext:self.context];
        newPage.thumbnail = imageData;
        [self.collectionView reloadData];
    }
    
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
        NSLog(@"Load request failed: %@", error);
    }
    Thoughts? I want to get a thumbnail of a page and save it to my database and then show the thumbnail instead of the page itself.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    For some reason you need to add the webView as a subview for the delegate methods to get triggered.
     
  3. ArtOfWarfare, Apr 13, 2013
    Last edited: Apr 13, 2013

    ArtOfWarfare thread starter macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    Are you saying that it actually does everything except call the delegate, or only that you know it doesn't call the delegate until it's added as a subview?

    If it's the former, do you have any idea how I could make this work despite that (IE, some other way I could find out when the page is finished.)

    Do you have any idea how I could make this work at all?

    Edit: Alright, I've got it working with this:

    Code:
        // get the thumbnail
        UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(largerScreenDimension, largerScreenDimension, largerScreenDimension, largerScreenDimension)];
        [self.view addSubview:webView];
        webView.delegate = self;
        objc_setAssociatedObject(webView, &pageKey, page, OBJC_ASSOCIATION_RETAIN);
        [webView loadRequest:[NSURLRequest requestWithURL:url]];
    (The pageKey is just because I have multiple webpages being rendered at once and I want to know which one is which.)

    Code:
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        Page *page;
        if ((page = objc_getAssociatedObject(webView, &pageKey))) {
            UIGraphicsBeginImageContext(webView.bounds.size);
            [webView.layer renderInContext:UIGraphicsGetCurrentContext()];
            UIImage *webViewImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            page.thumbnail = UIImagePNGRepresentation(webViewImage);
            [webView removeFromSuperview];
            [self.collectionView reloadData];
        }
    }
    I've attached a screenshot of what my app looks like so far... It has a few bugs + visually it's ugly... that seems to be the consequence of tons of people making really ugly websites, though.

    Yeah... it's mismatching various items somehow...
     

    Attached Files:

  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    The latter. I copied your code, only altered the webViewDidFinishLoad: to remove everything but the NSLog(), and added it to a simple single-view app. I confirmed the delegate methods were not being triggered. Then, I simply added
    Code:
    [self.view addSubview:webView];
    to the top chunk. I ran it again and now the NSLog() from the webViewDidFinishLoad: was output.
     

Share This Page