I'm trying to show a UIWebView with a GIF in it, but only once the GIF has loaded.
I load the GIF as follows:
self.GIFWebView = [[UIWebView alloc] init];
self.GIFWebView.delegate = self;
NSString *html = [NSString stringWithFormat:@"<html><head></head><body><img src=\"%@\"></body></html>", post.url];
[self.GIFWebView loadHTMLString:html baseURL:nil];
Where post
is just an object with some properties such as the URL for the GIF.
Then in webViewDidFinishLoad:
I show the web view:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"%f", webView.scrollView.frame.size.width);
NSLog(@"%@", [webView.request.URL absoluteString]);
}
I get "0" and "about:blank" for the NSLog
s each time, however.
Why does it not load the GIF properly?
Not surprising. You're telling the web view to load HTML that you're providing in a string rather than giving it a request. The URL that you're logging is the request URL, and since there's no request, there's no request URL.
Possibly because you're misusing the URL object. Look at the code:
We can't tell what type
post.url
is, but it's probablyNSURL*
. You're probably passing a NSURL into the format string, and that may not produce the result you're looking for. Try passing in a string like[post.url absoluteString]
instead of the actual NSURL object.Also, you might want to log the value of
html
right after you create it so that you can check the full HTML that you're sending to the web view.Update: Some additional things to check:
-webView:shouldStartLoadWithRequest:
method, does it return YES?+stringWithFormat:
?Update 2: The problem lies in your creation of the web view. Look at the very first line in the code that you showed:
That looks okay for a typical object, but
-init
is not the designated initializer for a view. You should use-initWithFrame:
instead. The image loads fine in your sample project when I change the code in your project to use the right initializer: