簡體   English   中英

如何在臨時目錄中保存圖像和錄制的文件?

[英]How to save images and recorded files in temp directory?

我想將來自我的應用程序的相機和視頻錄制的圖片存儲在臨時目錄中的單獨文件夾中一段時間​​。 當任務完成后,他們將要保存到數據庫中。

如何將攝像機和視頻錄制文件中拍攝的圖像保存在臨時目錄中的單獨文件夾中?

您正在尋找這個以獲取緩存文件夾來存儲臨時文件:

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

從那里,您可以使用NSFileManager來創建和刪除所需的目錄。 並且存儲文件就像文件系統的任何其他部分一樣。 請注意,系統會立即清除緩存目錄。

因此,在重新啟動應用程序之間,永遠不要依賴文件剩余,也不要消失。

嘗試這個:

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

如果要從臨時目錄中刪除所有文件,請使用此目的。

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

在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
}

從Temp目錄獲取和顯示圖像

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

您可以創建自己的Temp文件夾...

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

要將照片和視頻保存在不同的目錄中,您需要更改目錄路徑並保存數據庫的路徑。 檢查下面的代碼以在文檔目錄中創建文件夾並使用唯一名稱保存文件。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM