3

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

// 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] 数据路径是 /var/mobile/Applications/9676D920-D6D1-4F86-9177-07CC3247A124/Documents/tessdata 打开数据文件 /var/mobile/Applications/9676D920 时出错-D6D1-4F86-9177-07CC3247A124/Documents/tessdata/eng.traineddata

我的 eng.traineddata 文件位于我的 xcode 项目中,如下所示

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

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

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

4

4 回答 4

4
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;
}
于 2012-06-07T05:15:36.733 回答
1

我希望也帮助此代码将所有类型的文件包复制到文档目录文件夹..

NSString *pathsToReources = [[NSBundle mainBundle] resourcePath];
NSString *yourOriginalDatabasePath = [pathsToReources stringByAppendingPathComponent:@"lunchDB.sqlite"];  // file name u want copy 

NSArray *pathsToDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [pathsToDocuments objectAtIndex: 0];


    NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"lunchDB.sqlite"];  // file name & your original path (Document path)

    if (![[NSFileManager defaultManager] isReadableFileAtPath: dbPath]) {

if ([[NSFileManager defaultManager] copyItemAtPath: yourOriginalDatabasePath toPath: dbPath error: NULL] != YES)

NSAssert2(0, @"Fail to copy database from %@ to %@", yourOriginalDatabasePath, dbPath);

}

谢谢 ..

于 2014-05-01T06:18:30.900 回答
0

使用方法fileExistsAtPath:isDirectory:NSFileManager指定路径是目录,因为它是。

于 2012-06-06T14:32:57.830 回答
0

stringByAppendingPathComponent:@"tessdata"

上面的行要求 tessdata 是 Xcode 项目中的物理文件夹(蓝色文件夹),而不是组(您显示的黄色文件夹)。使用 Finder 在您的 Xcode 项目文件夹中创建一个文件夹,然后在 Xcode 中将该文件夹拖放到您的项目中,并在询问时选择文件夹引用。

于 2012-06-06T14:59:19.023 回答