将文件从 iOS 应用程序加载到 C++ 对象中/<iostream>iOS 上的问题
Loading a file into a C++ object from an iOS App / issues with <iostream> on iOS
我正在努力在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
。
希望有帮助。
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 使用ios:ate写入到流会覆盖现有文件
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 如何访问RTCVideoRenderer的帧?(ios)
- ios::fmtflags 在 C++ 中是如何工作的?setf() 是如何工作的?
- 我可以在 iOS 或 mac 应用程序中使用C++代码吗?
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 为什么 C++ 11 在 ios 类中添加了运算符 bool
- 'string'文件在带有C++文件的iOS插件上找不到
- 文件模式标志"ios::app"是否用于删除文件(如果文件已存在)?
- 使用 C++ iOS::ate 获取错误的文件大小
- 使用 适用于 Android 和 iOS 的 tf-lite C++ API
- 如何正确设置 ios 标志以进行流操作?
- 在为 iOS 构建 aws-sdk-cpp 时,cmake 上的 CXX 编译器错误已损坏
- C++ std::locale( "en" ) 在 iOS 上引发异常
- 在瓦尔格林德的泄漏摘要中使用 std::ios::sync_with_stdio(fasle) 打印时获取"still reachable"
- 在iOS应用程序中创建数据库
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到