How to save images and recorded files in temp directory?

I want to store pictures taken from the camera and videos from my application in a separate folder in temporary directories. And as tasks are completed, they will be saved in the database.

How to save pictures taken from cameras and videos in a separate folder in temp directories?

+6
source share
6 answers

You are looking for this to get a cache folder for storing temporary files:

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

From there, you can use NSFileManager to create and delete the necessary directories. And store files the same way as in any other part of the file system. Just keep in mind that the system will clean the cache directory now and then.

Therefore, never rely on a file remaining or not disappearing between restarts of your application.

+23
source

try the following:

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

If you want to delete all files from the temporary directory, 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]; } 
+2
source

To save an 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 the 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]; } } 
+2
source

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]; 
+1
source

To save photos and videos in different directories, you need to change the directory path and save the database path there. check the code below to create a folder in the document directory and save the file with a 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]]]; 
0
source

Source: https://habr.com/ru/post/893930/


All Articles