在退出程序之前保存的图像

问题描述:

我正在创建一个使用相机中图片的应用程序。在退出程序之前保存的图像

我要求用户拍照,然后将其保存在我的文档文件夹中。看起来程序退出时它不会被删除。

如何以及在哪里做得最好(appdelegate也许)?

如果你想让应用程序退出时删除文件,我建议你改用/ tmp目录。阅读documentation of File System

此外,如果你只是想删除的文件目录中的文件,从applicationWillTerminate做到这一点,如下所示:

NSArray *homePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *homeDir = [homePaths objectAtIndex:0]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSError *error=nil; 

    NSArray *contents = [fileManager contentsOfDirectoryAtPath:homeDir error:nil]; 

    for (NSString *file in contents) { 
     BOOL success = [fileManager removeItemAtPath:[homeDir stringByAppendingPathComponent:file] error:&error]; 

     if(!success){ 
      NSLog(@"couldn't delete file: %@",error); 
     } 
    } 
+0

您好,我只是试图把我的照片在tmp目录(似乎更好),但它不会在应用程序退出时删除我的文件。这不是很奇怪吗? – 2011-01-27 12:57:57

删除你的照片

- (void)applicationWillTerminate:(UIApplication *)application 
你的应用程序代理

;