[英]Get images from Documents directory to view on UIWebView
我想将图像从我的应用程序“文档”目录获取到标签中的UIwebview中。
Document目录位于应用程序根目录中:
/Users/myname/Library/Application Support/iPhone Simulator/7.1-64/Applications/E6B53F7B-CCC0-43A0-B1EB-D7C60E10E6CB/
图像位于:
/Users/myname/Library/Application Support/iPhone Simulator/7.1-64/Applications/E6B53F7B-CCC0-43A0-B1EB-D7C60E10E6CB/Documents/image.jpg
我的uiwebview代码是:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@", documentsDirectory];
NSURL *url = [NSURL URLWithString:filePath];
[webView loadHTMLString:HTMLMarkup baseURL:url];
src属性仅设置为图像名称,但这似乎不起作用。 我确定它与获取图像的相对路径有关,但是我不知道uiwebview上加载的页面根目录位于何处。
我以为上面的代码将uiwebview的根路径设置为Documents目录,因此我可以仅通过其名称来调用该图像。
有人知道这是什么问题吗?
令人困惑的是, +URLWithString:
方法不适用于本地文件。 您需要+fileURLWithPath:
这吸引了我几次。 文件在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.