How to save images and recorded files in temp directory?
Asked Answered
C

6

6

I want to store pictures taken from camera and video recordings from my application in a separate folder in temporary directories for a while. And as the task is being completed, they shall be going to save into the database.

How do I save pictures taken from camera & video recording files in a separate folder in temp directories?

Colophony answered 30/7, 2011 at 8:8 Comment(0)
T
24

You are looking for this to get tot he caches folder to store temporary files:

NSString* path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, 
                                                      NSUserDomainMask,
                                                      YES) lastObject];

From there you can use NSFileManager to create and remove the directories you want. And store files just as to any other part of a file system. Just be aware that the system will purge the caches directory now and then.

So never rely on file remaining, nor disappearing, between restarts of your app.

Taconite answered 30/7, 2011 at 10:16 Comment(3)
Compiler is giving me error like.., "_NSSearchPathForDirectoriesInDomain", referenced. Is any reference framework being missed to include?Colophony
@iApple - Sorry I missed an s is the function name. Updated the answer.Taconite
The accepted answer, although correctly solving the problem of the original question. Did not provide a true temporary directory. +1 for the right answerGehrke
E
8

try this:

NSString *tmpDirectory = NSTemporaryDirectory();
NSString *tmpFile = [tmpDirectory stringByAppendingPathComponent:@"temp.txt"];
NSLog(@"Temp File:%@", tmpFile);
Engird answered 29/4, 2013 at 23:55 Comment(1)
How validate if this tmpFile exist?Allwein
H
2

If you want to remove all files from temporary directory then use this.

//cleanup temp files
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *paths = NSTemporaryDirectory();

    NSArray *contents = [fileManager contentsOfDirectoryAtPath:paths error:NULL];
    NSEnumerator *e = [contents objectEnumerator];
    NSString *filename;

    //NSLog(@"paths: %@ ... contents: %@ ...",paths,  contents);

    while ((filename = [e nextObject]))
    {

        [fileManager removeItemAtPath:[paths stringByAppendingPathComponent:filename] error:NULL];
    }
Hatchett answered 8/6, 2014 at 12:48 Comment(0)
L
2

To save image in Temp Directory

-(void)savePhotoToAlbum:(UIImage*)imageToSave withName:(NSString*)imageName {
    NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
    NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:imageName] URLByAppendingPathExtension:@"png"];
    NSLog(@"fileURL: %@", [fileURL path]);
    NSData *pngData = UIImagePNGRepresentation(imageToSave);

    [pngData writeToFile:[fileURL path] atomically:YES]; //Write the file
}

To get and display images from Temp directory

NSString *tmpDirectory = NSTemporaryDirectory();
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *cacheFiles = [fileManager contentsOfDirectoryAtPath:tmpDirectory error:&error];
for (NSString *file in cacheFiles)
{
    NSLog(@"%@",file);
    error = nil;
    if ([file.pathExtension isEqual:@"png"]){
        NSString *filePath = [tmpDirectory stringByAppendingPathComponent:file];
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];
    }
}
Lyrebird answered 8/9, 2016 at 7:21 Comment(0)
C
1

You can create your own Temp folder...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Temp"];

[[NSFileManager defaultManager] createDirectoryAtPath:tempFolderPath withIntermediateDirectories:YES attributes:nil error:NULL];
Capers answered 30/7, 2011 at 12:48 Comment(1)
Better answer below, if you are trying to avoid the documents directory.Timotheus
O
0

To save Photo and video in different directory you need to change there directory path and save there path to your database. check below code to create folder in documents directory and save file with unique name.

NSError *error;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@“/images”];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{

    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

}

NSString *destinationPath = [dataPath stringByAppendingFormat:@"/output_%@.jpg", [dateFormatter stringFromDate:[NSDate date]]];
Overseas answered 8/9, 2016 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.