我有一个加载某些html的UIWebView。其中包含一个img标签,我正在将图像保存在应用程序文档文件夹中,并希望在html中显示该图像。我是这样做的:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *tempImgFN = [NSString stringWithFormat:@"tempImg%d.jpg",storyRowID];
NSString *tempImg = [documentsDir stringByAppendingPathComponent:tempImgFN];
[imageBlob writeToFile:tempImg atomically:NO];
tempImg = [tempImg stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
tempImg = [tempImg stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
[string appendString:[NSString stringWithFormat:@"<img style='max-width:120px' src=\"%@\" alt=\"\"/>",tempImg]];
imageBlob是带有图像数据的NSData。 图像已成功保存,其余HTML内容也能正常加载,但图片未显示。
如果将UIWebView的baseUrl设置为指向文档文件夹,则可以正常工作。 但我不想这样做,因为我希望baseURL是[NSBundle mainBundle] resourcePath]以访问一些JavaScript文件和CSS。
file:/%@
返回的是什么?它应该返回 index.html 所在的相对路径吗?在我的情况下,您的建议返回了:file:///%@
。 - sAguinaga