我想在iOS中加载本地资源图片,但无法在应用程序中显示(但是当我在NSLog中显示链接时)。
这是我的代码
NSBundle* myBundle = [NSBundle mainBundle];
NSString* imgurl = [myBundle pathForResource:@"list-gotit" ofType:@"png"];
htmltext = [NSString stringWithFormat:@"<html><img src='%@'></html>",
imgurl];
[self.postview loadHTMLString:htmltext baseURL:baseurl];
img src将加载我的数据/我的网址已经是正确的我认为(我使用的是NSLOG):
<img src='/private/var/mobile/Containers/Bundle/Application/66B9184B-796E-4404-B436-E387asd123132/NEWBIE.app/list-gotit.png'>
但不显示为图片。
感谢您的帮助。
答案 0 :(得分:5)
这对我有用:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"list-gotit" ofType:@"png"];
NSURL *imageURL = [NSURL fileURLWithPath:filePath];
NSString *absoluteURLString = [imageURL absoluteString];
[html appendFormat:@"<img src=\"%@\" />", absoluteURLString];
或者作为两个班轮:
NSString *imageURL = [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"list-gotit" ofType:@"png"]] absoluteString];
[html appendFormat:@"<img src=\"%@\" />", imageURL];
答案 1 :(得分:0)
我认为您错过了/
<img src="xxxxx" />
并且
NSURL *imgUrl = [NSURL fileURLWithPath:[myBundle pathForResource:@"list-gotit" ofType:@"png"]];
我做了一个测试:
NSBundle* myBundle = [NSBundle mainBundle];
NSURL *imgUrl = [NSURL fileURLWithPath:[myBundle pathForResource:@"list-gotit" ofType:@"png"]];
NSString *htmltext = [NSString stringWithFormat:@"<html><img src='%@'></html>",
imgUrl];
self.postview = [[UIWebView alloc] initWithFrame:CGRectMake(100, 100, 300, 300)];
[self.view addSubview:self.postview];
[self.postview loadHTMLString:htmltext baseURL:nil];
一切都好。
答案 2 :(得分:0)
您只需要更改以下代码。
htmltext = [NSString stringWithFormat:@"<html><img src=\"%@\"></html>",
imgurl];
这是因为&#39;%@&#39;你的代码不能正常使用\&#34;%@ \&#34;可能对你有所帮助。