2013-10-08 33 views
0

我在下面试过两种方法,但只有白色背景出现,没有图像显示。如何在UIWebView中加载图像

1.

NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"]; 
[myWebView loadHTMLString:[NSString stringWithFormat:@"<html><body><img src=\"file://%@\"></body></html>",path] baseURL:nil]; 

2.

NSString *path = [[NSBundle mainBundle] pathForResource:@"yourImage" ofType:@"png"]; 
//Creating a URL which points towards our path 
NSURL *url = [NSURL fileURLWithPath:path]; 
//Creating a page request which will load our URL (Which points to our path) 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
//Telling our webView to load our above request 
[webView loadRequest:request]; 

我认为都是正确的方法,但都是无用的我。 Plz帮助我。

+0

ya thanku.But我完成了第二种方法,我在做一个愚蠢的错误。 –

回答

0
  1. 使用图像数据(test_image.jpeg在束会在web视图被加载)

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test_image" ofType:@"jpeg"]; 
    NSData *data = [NSData dataWithContentsOfFile:filePath]; 
    [webView loadData:data MIMEType:@"image/jpeg" textEncodingName:@"utf-8" baseURL:nil]; 
    
  2. 加载为HTML内容

    NSURL *imageUrl = [[NSBundle mainBundle] URLForResource:@"test_image" withExtension:@"jpeg"]; 
    NSString *htmString = [NSString stringWithFormat:@"<!DOCTYPE html><html><body><img border=\"0\" src=\"%@\" width=\"304\" height=\"228\"></body></html>", [imageUrl absoluteString]]; 
    [webView loadHTMLString:htmString baseURL:nil]; 
    

正如我认为最好的办法是为加载HTML内容。如果你想要,你可以按照你的意愿设计html内容。

0

两者都适合我。

您确定您已将您的xib中的webview与IBOutlet链接起来吗?

@property (nonatomic, weak) IBOutlet UIWebView *webView; 

这样对您的XIB(或故事板):

enter image description here