Alex Rivera | Logout

Correct way to load image into UIWebView from NSData object

Asked 2010-05-13T15:13:12.620
17

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?

Edit
Report

1 Answer

3

I had the same problem and I found somewhere else that you have to provide a value in the baseURL parameter. I also had encoding set:

textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://localhost/"]];

When I had nil in the baseURL parameter it would not load. By putting something that's basically irrelevant in there the MS docs all worked.

answered 2012-09-19T18:39:42.150

Your Answer