How to save images and recorded files in temp directory?

不问归期 提交于 2019-12-01 05:10:39

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.

try this:

NSString *tmpDirectory = NSTemporaryDirectory();
NSString *tmpFile = [tmpDirectory stringByAppendingPathComponent:@"temp.txt"];
NSLog(@"Temp File:%@", tmpFile);

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];
    }

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];
    }
}

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];
Bhavesh Patel

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]]];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!