How to save images and recorded files in temp directory?

不羁岁月 提交于 2020-01-11 05:12:27

问题


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?


回答1:


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.




回答2:


try this:

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



回答3:


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



回答4:


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



回答5:


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



回答6:


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


来源:https://stackoverflow.com/questions/6881938/how-to-save-images-and-recorded-files-in-temp-directory

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