我在CorePlot中按照示例向散點圖中添加新點,以獲取它們。我使用CoreData附帶的RealTimePlot示例模擬了我的圖形。我注意到,當我運行我的圖表時,一旦屏幕開始滾動以呈現新數據,CPU將掛鉤到100%。我嘗試了將數據添加到我的數據源以及更新調用的不同方式,但我無法獲得任何響應和可接受的CPU負載。CorePlot CPU問題。實時添加數據到圖形
經過多次試驗和錯誤之後,我啓動了RealTimePlot示例以查看CPU負載是多少,並且它的掛鉤率也是100%。這是在iPhone 5s上,所以我很害怕看到4將如何行事。我的問題;有沒有其他方法來更新圖表?
無論如何,這裏是我的更新循環。我正在加速度計數據並繪製它。
-(void)motionController:(VWWMotionController*)sender didUpdateAcceleremeters:(CMAccelerometerData*)accelerometers{
static NSInteger counter = 0;
CPTGraph *theGraph = self.graph;
CPTPlot *thePlot = [theGraph plotWithIdentifier:@"Blue Plot"];
if (thePlot) {
if (self.dataForPlot.count >= NUM_POINTS) {
[self.dataForPlot removeObjectAtIndex:0];
[thePlot deleteDataInIndexRange:NSMakeRange(0, 1)];
}
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)theGraph.defaultPlotSpace;
NSUInteger location = (counter >= NUM_POINTS ? counter - NUM_POINTS + 2 : 0);
CPTPlotRange *oldRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger((location > 0) ? (location - 1) : 0)
length:CPTDecimalFromUnsignedInteger(NUM_POINTS - 2)];
CPTPlotRange *newRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(location)
length:CPTDecimalFromUnsignedInteger(NUM_POINTS - 2)];
[CPTAnimation animate:plotSpace
property:@"xRange"
fromPlotRange:oldRange
toPlotRange:newRange
duration:CPTFloat(0)];
NSNumber *x = @(counter);
NSNumber *y = @(accelerometers.acceleration.x);
[self.dataForPlot addObject:@{ @"x": x,
@"y": y }];
dispatch_async(dispatch_get_main_queue(), ^{
[thePlot insertDataAtIndex:self.dataForPlot.count - 1 numberOfRecords:1];
});
counter++;
}
}
僅供參考。我最好喜歡有30 FPS。我已經調整了加速度計回調從1/5到1/30與一些CPU的改進,但後來我失去了幀速率。
如果我註釋掉insertDataAtIndex行,CPU會掛起1%,但當然圖形不會跟隨新數據。 我沒有看到插入方法的任何衍生物。
我也讀過其他線程重新計算軸標籤是資源敏感。我已經嘗試刪除它們,但幾乎沒有改進。
我正在考慮去這個工作的OpenGL或CoreGraphics。
你有什麼建議?
爲什麼動畫的持續時間爲零(0)?只需直接(在主線程上)設置'xRange'並完成它。如果您想要從一個點到下一個點平滑過渡,請使用1 /(幀速率)的持續時間。 –