Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to load image into UIWebView from NSData object

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?

like image 640
rustyshelf Avatar asked May 13 '10 15:05

rustyshelf


5 Answers

I tested the code with PNG ("image/png"), JPG ("image/jpeg") and GIF ("image/gif"), and it works as expected:

[webView loadData:imageData MIMEType:imageMIMEType textEncodingName:nil baseURL:nil];

Now, what's wrong with your app?

  • the imageData is not a well-formed image data. Try opening the file with a web browser or an image editor to check it.
  • the MIME type is incorrect. Look at the first bytes of the data to determine the actual file type.
  • webView is not connected in IB, is nil, is hidden, is covered with another view, is off screen, has a CGRectZero frame, etc.
like image 97
Costique Avatar answered Nov 20 '22 06:11

Costique


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

like image 37
vodkhang Avatar answered Nov 20 '22 06:11

vodkhang


UIImage *screenshot= [UIImage imageAtPath:
                              [[NSBundle mainBundle] pathForResource:@"MfLogo_aboutus" ofType:@"png"]]; 
NSData *myData = UIImagePNGRepresentation(screenshot); 
[vc addAttachmentData:myData mimeType:@"image/png" fileName:@"logo.png"];
like image 40
pratap shaik Avatar answered Nov 20 '22 06:11

pratap shaik


You can load urlImage into webview which is not saved locally as shown below code

NSString *str = @"";
        str = [str stringByAppendingString:@"http://t3.gstatic.com/images?q=tbn:7agzdcFyZ715EM:http://files.walerian.info/Funny/Animals/funny-pictures-firefox-file-transfer-is-complete.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
        [webView loadData:data MIMEType:@"application/jpg" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://google.com"]];
like image 3
Madan Mohan Avatar answered Nov 20 '22 07:11

Madan Mohan


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.

like image 3
Brian W Avatar answered Nov 20 '22 08:11

Brian W