Note that application bundle files are read only.
Binary Saving and Loading
Example store, read and delete for camera images
//********** STORE IMAGE **********
- (void)imageStore:(UIImage *)image forKey:(NSString *)key
{
//----- WRITE IMAGE TO FILE -----
NSString *imagePath = pathInDocumentDirectory(key); //Get file path (using FileHelpers function)
NSData *d = UIImageJPEGRepresentation(image, 0.5); //Turn into JPEG data (compression quality is float value of 0=lowest to 1=highest)
[d writeToFile:imagePath atomically:YES]; //Write it (atomically writes to a temporary location before copying to permanent - good protection in case app should crash)
}
(more...)
Create Unique Filename
NSString *filePath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
(more...)
Output Text File Contents
Example converting a NSData file to a string for display
NSString *s = [[[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"File Contents = %@", s);
Using Archiving
When a class conforms to the NSCoding protocol it can be archived and later loaded into the application.
Using The Document Directory To Store Files
Does File Exist
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectory"];
path = [path stringByAppendingPathComponent:@"SomeFileName"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
Where To Store Files
Documents Directory
Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the <Application_Home>/Documents directory and will be automatically backed up by iCloud. (more…)



