Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,711
6,304
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: %@", @"https://www.macrumors.com");
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://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.
 
For some reason you need to add the webView as a subview for the delegate methods to get triggered.

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...
 

Attachments

  • Screen Shot 2013-04-13 at 10.45.33 AM.png
    Screen Shot 2013-04-13 at 10.45.33 AM.png
    78.5 KB · Views: 94
Last edited:
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?
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.