X0r0N
X0r0N

Reputation: 1934

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?

Upvotes: 0

Views: 1024

Answers (1)

Rich Tolley
Rich Tolley

Reputation: 3842

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

Upvotes: 1

Related Questions