gpt4 book ai didi

iphone - 如何在文档目录的子文件夹中创建文件?

转载 作者:太空狗 更新时间:2023-10-30 03:47:49 25 4
gpt4 key购买 nike

我正在文档目录中创建一个名为 newFolder 的文件夹。所以现在我想知道如何在我的文件夹中创建 txt 文件,该文件夹是用 newFolder 创建的我现在如何在文档目录中创建文件但如何在我的文件夹中创建文件由我创建的文件夹。我也想从 newFolder 中获取此文件并传递给 iPhone 中的邮件编辑器。

这是在我的文档文件夹中创建文件夹,但现在我想在此文件夹中创建文件名称 newFolder 帮助我完成此操作。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"newFolder"];
NSError *error = NULL;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

我写了这段代码,但我的 csv 文件是在 doument 目录文件夹中创建的,我想在我的文件夹中创建这个文件,该文件夹是 doument 目录,名称为 newFolder

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

NSString *filename = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"EmployeeBase.Csv"]];
NSError *error = NULL;
BOOL written = [csvString writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!written)
NSLog(@"write failed, error=%@", error);

最佳答案

这里是将图像保存在您自己的文件夹中的文档目录中的示例...

UIImage *imageForShare = [UIImage imageNamed:@"anyImage.jpg"];
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"];
// New Folder is your folder name
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

NSString *fileName = [stringPath stringByAppendingFormat:@"/image.jpg"];
NSData *data = UIImageJPEGRepresentation(imageForShare, 1.0);
[data writeToFile:fileName atomically:YES];

Swift 4.0 版本

guard let image = UIImage(named: "anyImage.jpg") else {return}
guard var stringPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first else {return}
stringPath = stringPath + "New Folder"

// New Folder is your folder name
if !FileManager.default.fileExists(atPath: stringPath) {
do {
try FileManager.default.createDirectory(atPath: stringPath, withIntermediateDirectories: false, attributes: nil)
} catch let error {
print(error.localizedDescription)
return
}
}

let fileName = stringPath + "/image.jpg"
let url = URL(fileURLWithPath: fileName)
if let data = UIImageJPEGRepresentation(image, 1.0) {
do {
try data.write(to: url)
} catch let error {
print(error.localizedDescription)
}
}

关于iphone - 如何在文档目录的子文件夹中创建文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10926113/

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