gpt4 book ai didi

objective-c - 无法将文件从包复制到文档目录,为什么 fileExistsAtPath 返回错误?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:46 25 4
gpt4 key购买 nike

我正在使用以下代码片段尝试将文件从我的应用程序资源目录复制到文档区域。我使用了 PocketOCR 中的以下内容Github 上的项目:

// Set up the tessdata path. This is included in the application bundle
// but is copied to the Documents directory on the first run.
NSString *dataPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"tessdata"];
NSFileManager *fileManager = [NSFileManager defaultManager];

NSLog(@"Datapath is %@", dataPath);
// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:dataPath ]) {
NSLog(@"File didn't exist at datapath...");
// get the path to the app bundle (with the tessdata dir)
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *tessdataPath = [bundlePath stringByAppendingPathComponent:@"tessdata"];
if (tessdataPath) {
[fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL];
}
}

这是我调用上面的命令得到的输出:

2012-06-06 14:53:42.607 MyApp[1072:707] Datapath is /var/mobile/Applications/9676D920-D6D1-4F86-9177-07CC3247A124/Documents/tessdata Error opening data file /var/mobile/Applications/9676D920-D6D1-4F86-9177-07CC3247A124/Documents/tessdata/eng.traineddata

我的 xcode 项目中有 eng.traineddata 文件,就像这样

似乎在尝试检查 fileExistsAtPath 时报告了错误,我不希望这会引发错误,而是返回 YES 或 NO。

是否有更简单的方法来复制 eng.traineddata 文件,或者我犯了一个可以在这里更正的错误?

I have the eng.traineddata file located in my xcode project like so

最佳答案

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;

NSString *dataPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"tessdata"];
NSLog(@"Datapath is %@", dataPath);

// If the expected store doesn't exist, copy the default store.
if ([fileManager fileExistsAtPath:dataPath] == NO)
{
NSString *tessdataPath = [[NSBundle mainBundle] pathForResource:@"eng" ofType:@"traineddata"];
[fileManager copyItemAtPath:tessdataPath toPath:dataPath error:&error];

}


-(NSString*) applicationDocumentsDirectory{
// Get the documents directory
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
return docsDir;
}

关于objective-c - 无法将文件从包复制到文档目录,为什么 fileExistsAtPath 返回错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10915859/

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