2011-09-16 92 views
20

爲什麼,如果我用NSTemporaryDirectory救我的圖像,將圖像保存到如何將圖像保存到我的應用程序的tmp目錄中?

在/ var /文件夾/ OG/oGrLHcAUEQubd3CBTs-1zU +++ TI/-Tmp-/

而不是到

/用戶/ MyMac /庫/應用程序支持/ iPhone模擬器/ 4.3.2 /應用/ A685734E-36E9-45DD-BBE7-0A46F8F91DAF的/ tmp

這裏是我的代碼:

-(NSString *)tempPath 
{ 
    return NSTemporaryDirectory(); 
} 

-(void) saveMyFoto 
{ 
    NSString *urlNahledu = [NSString stringWithFormat:@"%@%@%@",@"http://www.czechmat.cz", urlFotky,@"_100x100.jpg"]; 
    NSLog(@"%@", urlNahledu); 


    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlNahledu]]]; 

    NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.8f)]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSLog(@"%@ %@", paths, [self tempPath]); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"pkm.jpg"]; 

    [data writeToFile:localFilePath atomically:YES]; 
} 
+1

這篇文章解釋了iOS應用程序的所有目錄,保存,刪除http://kmithi.blogspot.in/2012/08/ios-application-directory-structure.html – mithilesh

+0

'UIImageJPEGRepresentation()'已經返回'NSData * ' - 爲什麼在使用它之前將它傳遞給'dataWithData:'?我想知道你是否知道我不知道的東西... – ArtOfWarfare

回答

3

因爲在模擬器中運行的應用程序是不是真的像沙盒在iOS設備。在模擬器上,來自Mac OS的臨時目錄在NSTemporaryDirectory()中返回 - 在iOS設備上,臨時目錄實際上位於沙箱內。 這種差異不應該讓你擔任應用程序開發人員。

-2

如果你想你的圖像存儲在/用戶/ MyMac /庫/應用程序支持/ iPhone模擬器/ 4.3.2 /應用/ A685734E-36E9-45DD-BBE7-0A46F8F91DAF的/ tmp

然後使用nshomedirectory( )給你的位置upto/Users/MyMac /庫/應用程序支持/ iPhone模擬器/ 4.3.2 /應用程序/ A685734E-36E9-45DD-BBE7-0A46F8F91DAF然後你只需把/ tmp和存儲您的圖像。

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                NSUserDomainMask, YES); 
NSString* docDir = [paths objectAtIndex:0]; 
NSString* file = [docDir stringByAppendingString:@"/tmp"]; 
+5

這不起作用,並且NSHomeDirectory()的文本說明和代碼使用NSDocumentDirectory因此,/ tmp目錄將成爲Documents目錄的一部分,這是錯誤的。 –

47

這是使用的URL直接獲得一個鏈接到tmp目錄的首選方法,然後返回該目錄中的文件URL(pkm.jpg):

雨燕4.0

let tmpURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) 
    .appendingPathComponent("pkm") 
    .appendingPathExtension("jpg") 
print("Filepath: \(tmpURL)") 

// Then write to disk 
if let url = tmpURL, let data = UIImageJPEGRepresentation(image, 0.8) { 
    // Use do-catch for error handling, disk can be full etc. 
    try? data.write(to: url) 
} 

雨燕3.1

let tmpURL = try! URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) 
        .appendingPathComponent("pkm") 
        .appendingPathExtension("jpg") 
print("Filepath: \(tmpURL)") 

請注意,不處理可能的錯誤!

夫特2.0

let tmpDirURL = NSURL.fileURLWithPath(NSTemporaryDirectory(), isDirectory: true) 
let fileURL = tmpDirURL.URLByAppendingPathComponent("pkm").URLByAppendingPathExtension("jpg") 
print("FilePath: \(fileURL.path)") 

目標C

NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES]; 
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"pkm"] URLByAppendingPathExtension:@"jpg"]; 
NSLog(@"fileURL: %@", [fileURL path]); 

注意,一些方法仍然要求作爲字符串的路徑,然後使用[fileURL path]返回路徑的字符串(如如上所示在NSLog中)。 當升級當前應用程序的所有文件夾:

<Application_Home>/Documents/ 
<Application_Home>/Library/ 

保證是從舊版本(不包括<Application_Home>/Library/Caches子目錄)保存。使用Documents文件夾查找您可能希望用戶有權訪問的文件以及Library文件夾,查找App使用且用戶看不到的文件。


另一個更長的路可能獲得一個網址到tmp目錄,首先,把文件目錄和剝離的最後一個路徑組件,然後添加tmp文件夾:

NSURL *documentDir = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject]; 
NSURL *tmpDir = [[documentDir URLByDeletingLastPathComponent] URLByAppendingPathComponent:@"tmp" isDirectory:YES]; 
NSLog(@"tmpDir: %@", [tmpDir path]); 

然後我們能夠針對文件存在,即pkm.jpg如下所示:

NSString *fileName = @"pkm"; 
NSURL *fileURL = [tmpDir URLByAppendingPathComponent:fileName isDirectory:NO]; 
fileURL = [fileURL URLByAppendingPathExtension:@"jpg"]; 

可以使用字符串完成相同的操作,但在上面的第一個URL方法是現在推薦的方法(除非您正在寫入較舊的系統:iPhone OS 2或3):

NSString *tmpDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; 
tmpDir = [[tmpDir stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"tmp"]; 
NSString *filePath = [[tmpDir stringByAppendingPathComponent:@"pkm"] stringByAppendingPathExtension:@"jpg"]; 
+0

當我是一個視頻文件.mp4時,我無法從'NSTemporaryDirectory()'獲取文件。適用於音頻和圖像。幫助 – jose920405

+0

@ jose920405我真的不明白你的問題,但它可能與該文件是否受iOS支持有關。 –

+0

'isDirectory'很重要 – onmyway133

相關問題