SQLite data fetch from Document directory - ios

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

Related

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

How can I get the file path from a UIImage?

Usually it's the other way around, you use the path to display the image. I was wondering if you can get the path if you already have the image.
if you already have the image i.e. have added the file to your resources, you can use this to get the file path;
NSString *string = [[NSBundle mainBundle] pathForResource:#"IMAGE_FILE_NAME" ofType:#"jpg"]; // or ofType:#"png", etc.
I don't believe it is possible to get it directly from UIImage.
Best way is to save the image in a directory, then you will have the file path.
//image is your UIImage;
if (image != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
#"test.png" ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
path will be your filepath.
Once a UIImage is created, the image data is loaded into memory and no longer connected to the file on disk. As such, the file can be deleted or modified without consequence to the UIImage and there is no way of getting the source path from a UIImage.

Retrieve by specific file name from document directory in iOS

Now i am retrieving file from document directory by specific name in iOS with following code.
NSMutableArray *arrayToSearch = [[NSMutableArray alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError * error;
arrayToSearch = (NSMutableArray *)[[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithFormat:#"%#/Manual.txt",documentsDirectory] error:&error];
I am sure i have the Manual.txt file in document directory.
However it doesn't show anything in tableView.
I also reload tableView.
Is there anything wrong?
The method is contentsOfDirectoryAtPath:. Read the name of the method. Read the description in the docs. The path you pass must reference a directory, not a file.
What you are trying to do doesn't make sense logically. If you know a specific file, then why search for it? Why create an array?
If you want to see if the file exists, use the fileExistsAtPath: method of NSFileManager.
If you just want the filename in the array then do:
NSString *filename = [documentsDirectory stringByAppendingPathComponent:#"Manual.txt"];
[arrayToSearch addObject:filename]; // since the array was pre-allocated
Please don't use stringWithFormat to create the filename. Use the proper NSString path methods like I did above.

Using directory to store information and keep this information in a App Update

I have an app that stores images from the user in the app/documents folder, I'm doing the following code determine the path to save it:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString *imageFilename = [[[[NSNumber alloc] initWithInteger:self.nextID] stringValue] stringByAppendingFormat:#".jpg"];
NSString *dirPath = [docs stringByAppendingFormat:#"/Photos/"];
imagePath = [dirPath stringByAppendingString:imageFilename];
it gives me a path similar to (in simulator):
/var/mobile/Applications/C9C43CFD-6A5F-40CF-8BAE-20496B5A544A/Documents/Photos/1.jpg
I save this path to show this image when I need.
My problem is, when I sent an update in the app store, after update the app it cant show the images anymore.
My understand is the Documents folder cant change when update the app in AppStore.
My probably reason for that it the name between Applications folder and Documentar has changed, is it true when update a app version from AppStore?
Does anyone has any idea? is there any way to test this app update local?
Thanks
You need to save the relative path after the Documents directory, not the absolute path. The app's directory can change during an update but the structure inside the app's sandbox won't change.
Also, there is a slightly better way to build your path:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString *imageFilename = [NSString stringWithFormat:#"%d.jpg", self.nextID];
NSString *dirPath = [docs stringByAppendingPathComponent:#"Photos"];
imagePath = [dirPath stringByAppendingPathComponent:imageFilename];
When the app is updated from the App Store, the contents of the Documents directory are unchanged. The app bundle and all its contents are replaced with the new version, but the Documents folder is a safe place to store content between app versions.

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