gpt4 book ai didi

iphone - 检查文档目录中是否存在文件名

转载 作者:太空狗 更新时间:2023-10-30 03:34:24 26 4
gpt4 key购买 nike

在我的应用程序中,我使用以下代码将图像/文件保存到应用程序的文档目录中:

-(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");
}

但是,图片名称有问题。如果文档目录中存在文件,则同名的新文件将覆盖旧文件。如何检查文档目录中是否存在该文件名?

最佳答案

使用 NSFileManagerfileExistsAtPath:检查它是否存在的方法。

用法

if ( ![fileManager fileExistsAtPath:filePath] ) {
/* File doesn't exist. Save the image at the path */
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
} else {
/* File exists at path. Resolve and save */
}

关于iphone - 检查文档目录中是否存在文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6491290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com