开发者

How to save images and recorded files in temp directory?

开发者 https://www.devze.com 2023-03-23 19:12 出处:网络
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 t

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?开发者_运维技巧


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


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

精彩评论

暂无评论...
验证码 换一张
取 消