简体   繁体   中英

Get images from Documents directory to view on UIWebView

i want to get images from my apps Documents directory into the UIwebview in tag.

the Document directory sits in the application root:

/Users/myname/Library/Application Support/iPhone Simulator/7.1-64/Applications/E6B53F7B-CCC0-43A0-B1EB-D7C60E10E6CB/

and the image sits in:

/Users/myname/Library/Application Support/iPhone Simulator/7.1-64/Applications/E6B53F7B-CCC0-43A0-B1EB-D7C60E10E6CB/Documents/image.jpg

my code for the uiwebview is:

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];

The src attribute is set to just the image name, but that doesn't seem to work. I'm sure its something about the relative path to get to the image, but I don't know where the root of the page loaded on the uiwebview is located.

I thought the code above sets the root path of the uiwebview to the Documents directory so I could call for the image simply by its name.

Does anyone know what the problem is here?

Confusingly, the +URLWithString: method will not work for local files. You need +fileURLWithPath: . This has caught me out a couple of times. Docs here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM