记录到文件加速度计数据

发布于 2024-09-10 03:22:29 字数 113 浏览 2 评论 0原文

我想添加一个Apple示例[AccelerometerGraph],能够将数据存储在文件中,直到应用程序未关闭。没有锁定问题和其他实时数据收集的蹩脚问题。

有什么想法、示例或半新手可以玩的东西吗?

I would like add a this Apple example [AccelerometerGraph] the ability to store the data in a file until the app isn't closed. Without locking issues and other crappy problems of real time data collection.

Any idea, sample or something that a semi newbie can play with?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

—━☆沉默づ 2024-09-17 03:22:29

您可以在加速度计数据命中时将其记录到 NSMutableArray 中,然后当应用程序关闭时将该数组写入文件中:

[array writeToFile:DataPath atomically:YES];

只是想确保您不存储每个条目,否则它可能会很快变得很大。对加速对象中的时间戳进行一些检查,使其

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 

每隔几秒记录一次,具体取决于您要运行它的时间。 (这种方法在不限制它的情况下长时间运行可能不明智,或者进行其他一些数组操作,以便在应用程序的整个生命周期中整个数组不会保存在内存中)

快速读取文件的方法已经写了。

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *DataPath = [documentsDirectory stringByAppendingPathComponent:@"SOMEFILENAME"];
    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:DataPath];
if([array count]<1){
//do something with data
}

you could log the accelerometer data into an NSMutableArray as it hits and then when the app is being closed write that array to a file with:

[array writeToFile:DataPath atomically:YES];

just want to make sure you dont store every single entry or it could get large very fast. Do some checking on the timestamp in the acceleration object in

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 

make it log every couple of seconds depending on how long you are going to be running it. (this method would probably not be wise to run for long lengths of time without throttling it, or doing some other array manipulations so that the entire array isnt held in memory through out the life time of the app)

quick way to read the file you have written.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *DataPath = [documentsDirectory stringByAppendingPathComponent:@"SOMEFILENAME"];
    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:DataPath];
if([array count]<1){
//do something with data
}
暖风昔人 2024-09-17 03:22:29

如果您只想查看输出的数据,您可以采用简单的方法将其记录到控制台。

在 MainViewController.m 中的 -(void)accelerometer: 方法中,只需添加

[filtered addX:filter.x y:filter.y z:filter.z];  // this line is already there
NSLog(@"%f, %f, %f", acceleration.x, acceleration.y, acceleration.z);

然后,当您在设备上运行它时,打开调试器控制台,它将为您流出数据。这是我得到的:

2010-07-13 22:15:45.187 AccelerometerGraph[11241:307] 0.072449, -0.018112, -1.068619
2010-07-13 22:15:45.203 AccelerometerGraph[11241:307] 0.072449, -0.018112, -1.068619
2010-07-13 22:15:45.219 AccelerometerGraph[11241:307] 0.072449, 0.000000, -1.050507
2010-07-13 22:15:45.236 AccelerometerGraph[11241:307] 0.054337, -0.018112, -1.032394
2010-07-13 22:15:45.254 AccelerometerGraph[11241:307] 0.054337, -0.018112, -1.014282
2010-07-13 22:15:45.272 AccelerometerGraph[11241:307] 0.072449, -0.018112, -1.050507
2010-07-13 22:15:45.287 AccelerometerGraph[11241:307] 0.072449, -0.018112, -1.068619

If you are just wanting to view the data as it comes out, you can do the simple way of just logging it to the console.

In MainViewController.m, in the -(void)accelerometer: method just add

[filtered addX:filter.x y:filter.y z:filter.z];  // this line is already there
NSLog(@"%f, %f, %f", acceleration.x, acceleration.y, acceleration.z);

Then when you run it on the device, open up the debugger console and it will stream out the data for you. Here is what I got:

2010-07-13 22:15:45.187 AccelerometerGraph[11241:307] 0.072449, -0.018112, -1.068619
2010-07-13 22:15:45.203 AccelerometerGraph[11241:307] 0.072449, -0.018112, -1.068619
2010-07-13 22:15:45.219 AccelerometerGraph[11241:307] 0.072449, 0.000000, -1.050507
2010-07-13 22:15:45.236 AccelerometerGraph[11241:307] 0.054337, -0.018112, -1.032394
2010-07-13 22:15:45.254 AccelerometerGraph[11241:307] 0.054337, -0.018112, -1.014282
2010-07-13 22:15:45.272 AccelerometerGraph[11241:307] 0.072449, -0.018112, -1.050507
2010-07-13 22:15:45.287 AccelerometerGraph[11241:307] 0.072449, -0.018112, -1.068619
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文