[英]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.