Get images from Documents directory to view on UIWebView - ios

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

Related

iMessage App: Sticker from ImageFile from disk can be peeled of, but not placed

My standalone Message App saves images like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:[imageName stringByAppendingString:#".png"]];
The PNG files are than loaded into MSStickers and correctly displayed in a MSStickerBrowserView. When I try to place the sticker in a conversation, the sticker disappears after a second. It works fine with my test image inside the Resources folder:
[[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
Does anyone know what kind of problem this is? There is no error message either.
Very simple answer: The saved image files where larger than allowed. After shrinking it works

Play audio from created directory

Hi I'm using the following code to play an audio file
NSString *stringPath = [[NSBundle mainBundle]pathForResource:#"audioFileName" ofType:#"mp3" inDirectory:#"/Downloads"];
NSURL *url = [NSURL fileURLWithPath:stringPath];
NSError *error;
avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
avPlayer.delegate = self;
[avPlayer play];
But I get an error because my audio files I located in a folder called "Downloads"
Path: AppName/Library/ApplicationSupport/Downloads/audioFileName.mp3
The error I get is that the string stringPath is nil probably because that line of code is written wrong. I know for sure the file is there!
Heres an image using file browsing software
So my question how do i modify the above code to point to my downloads directory when playing a audio file. Thanks
Ive even tried putting the whole path like this.
NSString *stringPath = [[NSBundle mainBundle]pathForResource:#"audioFileName" ofType:#"mp3" inDirectory:#"Library/Application Support/Downloads"];
Status update!
I now do the following and the error is gone but not sure if its pointing to the correct folder still and if so is it possible I don't have permissions to use the files within it. This is a directory i created so is changing permissions to it after its created something I had too do?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
NSString *stringPath = [[libraryDirectory stringByAppendingPathComponent:#"Downloads"] stringByAppendingPathComponent:#"audioFileName.mp3"];
I believe pathForResource is only for files you include with your app. You have made the audio file in the Library folder which is not in your .app folder... so you could try to use something like:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
NSString *stringPath = [[libraryDirectory stringByAppendingPathComponent:#"Downloads"] stringByAppendingPathComponent:#"audioFileName.mp3"];

How to read pdf file from document directory with timestamp in iPhone?

i m having a problem i have too many pdf files so one by one i m downloading pdf files,
so i put timestamp with my document folder so mypath look like this:
/Users/zee/Library/Application Support/iPhone Simulator/6.0/Applications/10EF6B94-0ECD-484A-
AF5A-D300D8EF55DB/Documents/myPDF.pdf1382698338.78128
but i dont understand why its not opening in UIWebView with timestamp, my below code is
working when i dont put the timestamp.
so how can i open my pdf file that is attached with the timestamp below is my code please
help me out how to solve this problem
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"myPDF.pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.webView loadRequest:request];
my filepath is :
/Users/zee/Library/Application Support/iPhone Simulator/6.0/Applications/10EF6B94-0ECD-484A-
AF5A-D300D8EF55DB/Documents/myPDF.pdf1382698338.78128
Please tell me how can i open my pdf file
so i put timestamp with my document folder so mypath look like this:
/Users/zee/Library/Application Support/iPhone Simulator/6.0/Applications/10EF6B94-0ECD-484A-AF5A-D300D8EF55DB/Documents/myPDF.pdf1382698338.78128
You should try using a different scheme for naming you files, so that the extension is kept unmodified, i.e.:
..../myPDF<TIMESTAMP>.pdf
e.g.
..../myPDF1382698338.78128.pdf

SQLite data fetch from Document directory

I have a sqlite db stored in the Document directory and images name in one of the column of table ..and Image are store in app bundle.Images are coming directly by giving the names of imgaes in the column by using code..means no need to specify any path..just name is fine.
now if i store images dynamically in the document directory and give the name of the image in image column of Sqlite db directly as above I am unable to fetch it. Do i need to specify any path to reach there..as In app bundle images are coming directly.
Yes. You need to specify the path to the sqlite file in the document directory.
Or use the following code with corresponding image name (Images are in document directory):
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"img.png"];
yourImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]];
You can get directory path as below:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
and to get image you have to append image name after this path. You have data in your database, so from that you can get imagename.
If the value in the database is just the name of the image (such as "someimage.png") then you have two possibilities:
1) If the image is in the app bundle then use the value from the database with the UIImage imageNamed: method. I believe you already have this working.
2) If the image is in the Documents directory then you have a little more to do. You need to obtain the full path to the Documents directory and then append the filename (from the database) to the path. This will give you a full path to the image in the Documents directory. Then to load the image you need to use the UIImage imageWithContentsOfFile: method passing in the full file path.
Use this code .
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory35 = [paths objectAtIndex:0];
//Essential Info
NSString *fullPathEssentialInfo = [documentsDirectory35 stringByAppendingPathComponent:#"EssentialInfo.txt"];
This is implemented code.Thanks
There is an example project for accessing SQLite here: https://github.com/AaronBratcher/ABSQLite

How can I permanently save AVAudioRecorder files for later access?

I have my code all set up to actually record the files. What I need, though, is a way to see where they are all saved and list them in a UITableView. I'm not asking the the implementation for the table; I only need a way to see where they are all saved.
All your data is saved into your application's document folder. You can access it with:
NSString *documentsDirectory;
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] > 0) {
documentsDirectory = [paths objectAtIndex:0];
}
Assign an array to get a list of all your recorded files.
Your url where you are going to save the recorded file:
NSURL *urlOfRecordedFile = [NSURL fileURLWithPath:[NSString stringWithFormat:#"5#/recordFile.caf",[[NSBundle mainBundle] resourcePaht]]];
And here is the actual path of you file
NSLog(#"%#",url);

Resources