2010-05-13 74 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]; 
相關問題