2010-05-13 76 views
16

我已经下载到NSData对象(我检查了NSData对象的内容,它绝对填充)的gif图像。现在我想将该图像加载到我的UIWebView中。我试过以下内容:正确的方式来从NSData对象加载图像到UIWebView

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

但我得到一个空白的UIWebView。直接从同一个URL加载图像正常工作:

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

我需要的textEncodingName设置的东西,还是我做别的事情了?

我想手动加载图像,以便我可以向用户报告进度,但它是一个动画gif,所以完成后我想在UIWebView中显示它。

编辑:也许我需要以某种方式包装我的图像在HTML中?有没有办法做到这一点,而不必将其保存到磁盘?

+0

生锈的,我用的是第一行代码的显示本地GIF图像,它工作正常。看起来方法没问题。 – 2010-09-13 06:21:01

+0

嗨,你找到了你的问题的答案? – 2012-05-03 23:28:33

回答

6

我没有真正尝试加载图像到UIWebView,但谷歌搜索给了我。我觉得你的形象字符串必须有一个良好的路径,看起来像一个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]]]; 

你可以看到更多的细节在这里:Loading local files to UIWebView

+0

我不认为可以帮助我,除非我在本地保存图像? – rustyshelf 2010-05-14 01:22:35

+0

对不起,我以为你想在本地保存。这是我的错:( – vodkhang 2010-05-14 03:54:00

+0

好的回答.... – TheTiger 2013-05-29 05:31:49

1

你可能想尝试指派代表到网页视图和实施方法:

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

要更具体地了解您遇到的错误。如果它不被调用,实现方法:

- (void)webViewDidFinishLoad:(UIWebView *)webView 

为好,只是为了确保东西正在发生的事情,否则可能出现的问题与UIWebView(假设你没有从webView:shouldStartLoadWithRequest:navigationType:返回NO

+0

它不会失败,但它确实会调用finishload方法。也许你不能只传递它的图像数据,你需要包装它在HTML中以某种方式? – rustyshelf 2010-05-14 01:22:02

+0

如果您想尝试将其封装在HTML中,您可以始终使用data:URL,它将图像(或其他任何东西)编码为64位数据,参见http:// http://例如en.wikipedia.org/wiki/Data_URI_scheme#HTML – 2010-05-15 22:14:36

3

可以urlImage加载到其如下所示代码

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"]]; 
0

要在埃德马蒂的评论扩大没有本地保存网页流量:

的HTML命令来使基部64,图像是:

<img src="data:image/png;base64,##PUT THE BASE64 DATA HERE###" /> 

我有一个类(我不知道哪里来的,不是我来了......)我的网站上可用的转换NSData是Base64的字符串表示。

Header Implementation

足够容易做到的,假定 '的imageData' 是包含图像NSData的变量: [的imageData base64Encoding]到上述字符串。

+0

这些链接不再工作了吗?你碰巧知道他们去哪里了吗? – 2012-12-17 10:22:46

0

试试这个代码

// 1) Get: Get string from “outline.plist” in the “DrillDownSave”-codesample. 
savedUrlString = [item objectForKey: @"itemUrl"]; 

// 2) Set: The url in string-format, excluding the html-appendix. 
NSString *tempUrlString = savedUrlString; 

// 3) Set: Format a url-string correctly. The html-file is located locally. 
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:tempUrlString ofType:@”html”]; 

// 4) Set: Set an “NSData”-object of the url-sting. 
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile]; 

// 5. Gets the path to the main bundle root folder 
NSString *imagePath = [[NSBundle mainBundle] resourcePath]; 

// 6. Need to be double-slashes to work correctly with UIWebView, so change all “/” to “//” 
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"]; 

// 7. Also need to replace all spaces with “%20″ 
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 

// Load: Loads the local html-page. 
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//",imagePath]]]; 
+9

你可能想指出你已经复制了这个单词从这里:http://forums.macrumors.com/showthread.php?t=613532 – 2011-01-23 03:48:35

22

我测试的代码PNG( “图像/ PNG”),JPG( “图像/ JPEG”)和GIF( “图像/ GIF”),并且它可以作为预计:

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

现在,您的应用出了什么问题?

  • imageData不是一个格式良好的图像数据。尝试使用网络浏览器或图像编辑器打开文件进行检查。
  • MIME类型不正确。查看数据的第一个字节以确定实际的文件类型。
  • Webview是没有连接在图1B中,为nil,是隐藏的,覆盖有另一视图,处于关闭状态的屏幕,具有CGRectZero帧等
+0

我想显示PNG file.is它可能使用此代码? – 2016-12-29 14:11:17

4
UIImage *screenshot= [UIImage imageAtPath: 
           [[NSBundle mainBundle] pathForResource:@"MfLogo_aboutus" ofType:@"png"]]; 
NSData *myData = UIImagePNGRepresentation(screenshot); 
[vc addAttachmentData:myData mimeType:@"image/png" fileName:@"logo.png"]; 
+0

@什么是VC .... – 2016-12-29 14:17:39

3

我有同样的问题,我发现在其他地方,您必须在参数baseUR中提供一个值。我也有编码设置:

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

当我在baseURL参数就不会加载了nil。通过把那些基本上不相关的东西放在那里,MS文档都起作用了。

0

下面是另一种方法:

将下载的图像保存到文档文件夹中。 然后获取该图片的网址。然后在IMG SRC标签中使用该图像url编写一个简单的html文件 。

NSLog(@"url=%@", fileURL); // fileURL is the image url in doc folder of your app 


//get the documents directory: 
NSArray *paths = NSSearchPathForDirectoriesInDomains 
(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

//make a file name to write the data to using the documents directory: 
NSString *fileName = [NSString stringWithFormat:@"%@/toOpen.html", 
         documentsDirectory]; 

//create simple html file and format the url into the IMG SRC tag 
NSString *content = [NSString stringWithFormat:@"<html><body><img src=%@></body></html>",fileURL]; 
//save content to the documents directory 
[content writeToFile:fileName 
      atomically:NO 
      encoding:NSStringEncodingConversionAllowLossy 
       error:nil]; // now we have a HTML file in our doc 

// open the HTML file we wrote in the webview 

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"life.html"]; 
NSURL *url = [NSURL fileURLWithPath:filePath]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
[yourWebView loadRequest:request]; 
0
NSString *pathForFile = [[NSBundle mainBundle] pathForResource: @"fireballscopy" ofType: @"gif"]; 
    NSData *dataOfGif = [NSData dataWithContentsOfFile: pathForFile]; 
    [Web_View loadData:dataOfGif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil]; 
相关问题