将文件从 iOS 应用程序加载到 C++ 对象中/<iostream>iOS 上的问题

Loading a file into a C++ object from an iOS App / issues with <iostream> on iOS

本文关键字:iOS lt iostream 问题 gt 应用程序 文件 加载 对象 C++      更新时间:2023-10-16

我正在努力在iOS应用程序中集成一个C++库(GRT,特别是机器学习工具包(。

我已经将GRT构建为框架,包括使用一些Objective-C++包装器函数在我的应用程序和框架之间调用。

目前,我正在尝试解决涉及文件加载的问题。具体来说,我正在尝试将文件从我的应用程序包加载到 GRT 模块中。

这是我获取要访问的文件并初始化 GRT 包装器的地方:

func loadTrainingData(){
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileUrl = documentsUrl.appendingPathComponent("train.grt")
let pipeline = GestureRecognitionPipeline()
let test:Bool = pipeline.load(fileUrl.path)
print(test)
}

下面是调用pipeline.load时调用的 Obj-C++ 包装器代码:

- (BOOL)load:(NSString *) path
{
BOOL result = self.instance->load(std::string(path.UTF8String));
if (result) {
std::cout << "GRT config";
std::cout << self.instance->getModelAsString();
std::cout << "GRT info: " << self.instance->getInfo();
}
return result;
}

最后,以下是 GRT 库的实际C++代码,其中处理文件加载:

bool GestureRecognitionPipeline::load(const std::string &filename){
std::fstream file;        
//Clear any previous setup
clear();
file.open(filename.c_str(), std::iostream::in );
if( !file.is_open() ){
errorLog << __GRT_LOG__ << " Failed to open file with filename: " << filename << std::endl;
return false;
}
...
}

目前,我总是无法让我的管道对象成功导入文件。我认为这不一定与我在iOS端访问文件的方式有关(尽管我可能是错的(。有人有见识吗?任何赞赏 - 谢谢!

编辑:我能够通过此检查验证我正在加载我的文件是否正确加载:

let path = Bundle.main.path(forResource: "acc-orientation", ofType: "grt") print(path as Any!)

但是,我仍然遇到与以前相同的问题。

编辑 2我验证了路径是否也在 Obj-C++ 包装器中正确加载; 这让我认为这可能与 iOS 中的处理方式有关......现在完全迷失在这里...

编辑 3正如一位同事所建议的那样,我尝试使用文件 url 的absoluteString传递给我的包装器和底层C++代码,因为C++无法访问 iOS 的沙盒环境。结果还是一样:

let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileUrl = documentsUrl.appendingPathComponent("acc-orientation.grt")
let pipeline = GestureRecognitionPipeline()
let test:Bool = pipeline.load(fileUrl.absoluteString)

编辑 4正如评论中所建议的,我尝试使用fileSystemRepresentation,但这也没有带来成功。

- (BOOL)load:(NSURL *) url {
BOOL result = self.instance->load(std::string([url fileSystemRepresentation]));
...
}

编辑5:我做了一个简单的测试项目,试图只访问文件并使用Swift->Obj-C++->C++加载它(换句话说,没有框架文件(。这是一个可以下载的链接。任何帮助表示赞赏!

好吧,你快到了。我已经下载了您的示例项目并使其正常工作。您的问题与要打开的文件的实际位置有关。目前,您正在尝试从"文档"文件夹中打开文件,但实际上从未将文件从 App Bundle 复制到"文档"文件夹。所以有两种解决方案:

解决方案 1:应用捆绑包

更改ViewController.swift中的loadTrainingData方法以从应用程序捆绑包访问文件:

func loadTrainingData(){
let path = Bundle.main.url(forResource: "acc-orientation", withExtension: "grt")
let wrapper = Wrapper()
let test:Bool = wrapper.load(path)
print(test)
}

解决方案 2:文档文件夹

首次启动后,立即将文件从应用程序捆绑包复制到"文档"文件夹。因此,请将以下代码片段复制到您的AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
do {
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("acc-orientation.grt")
let bundleURL = Bundle.main.url(forResource: "acc-orientation", withExtension: "grt")
try FileManager.default.copyItem(at: bundleURL!, to: url)
} catch {
print("File already exists")
}
return true
}

使用这两种解决方案中的任何一种,您的FileLoader::load方法都将返回true

希望有帮助。