views:

687

answers:

2

I have downloaded a gif image into an NSData object (I've checked the contents of the NSData object and it's definitely populated). Now I want to load that image into my UIWebView. I've tried the following:

[webView loadData:imageData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];

but I get a blank UIWebView. Loading the image from the same URL directly works fine:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]];
[imageView loadRequest:request];

Do I need to set the textEncodingName to something, or am I doing something else wrong?

I want to load the image manually so I can report progress to the user, but it's an animated gif, so when it's done I want to show it in a UIWebView.

Edit: Perhaps I need to wrap my image in HTML somehow? Is there a way to do this without having to save it to disk?

A: 

I did not really try to load image to UIWebView but a google search gives me. I think your image string must have a good path and looks like a URL

NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

NSString *HTMLData = @"
<h1>Hello this is a test</h1>
<img src="sample.jpg" alt="" width="100" height="100" />";
[webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]];

You can see more details here : Loading local files to UIWebView

vodkhang
I don't think helps me unless I save the image locally?
rustyshelf
oh sorry, I thought that you wanted to save locally. It was my fault.:(
vodkhang
A: 

You may want to try assigning a delegate to the webview and implementing the method:

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

To see more specifically what error you're getting. If it doesn't get called, implement the method:

- (void)webViewDidFinishLoad:(UIWebView *)webView

as well, just to make sure something is happening, otherwise there might be an issue with UIWebView (assuming you haven't returned NO from webView:shouldStartLoadWithRequest:navigationType:)

Ed Marty
it doesn't fail with an error, but it does call the finishload method. Perhaps you can't just pass it the data for the image, you need to wrap it in HTML somehow?
rustyshelf
If you want to try wrapping it in HTML, you can always use a data: URL, which base-64-encodes images (or anything, really) as data in the HTML document. See http://en.wikipedia.org/wiki/Data_URI_scheme#HTML for an example.
Ed Marty