【发布时间】:2011-09-23 09:26:27
【问题描述】:
在我的应用程序中,我使用以下代码将图像/文件保存到应用程序的文档目录中:
-(void)saveImageDetailsToAppBundle{
NSData *imageData = UIImagePNGRepresentation(userSavedImage); //convert image into .png format.
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",txtImageName.text]]; //add our image to the path
NSLog(fullPath);
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the image
NSLog(@"image saved");
}
但是,图片名称有问题。如果文档目录中存在文件,则同名的新文件将覆盖旧文件。如何检查文件名是否存在于文档目录中?
【问题讨论】:
标签: iphone objective-c cocoa-touch