OStack程序员社区-中国程序员成长平台

标题: ios - 加载包含 Core Plot 图的 View Controller 时延迟过大 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:46
标题: ios - 加载包含 Core Plot 图的 View Controller 时延迟过大

我刚开始使用 Core Plot,为了测试,在一个简单的自定义 View Controller 中嵌入了一个 CPTGraphHostingView,绘制来自 Core Data fetchRequest 的值(这是一个绘制每天膳食卡路里摄入量的应用程序)。

代码大部分是从教程 here 中粘贴的.

问题是当将 View Controller 插入 View 时 UI 卡住了大约两秒钟(它嵌入在导航 Controller 中)。这是在设备 (iPhone 4S) 上运行时。

Instruments 中的分析显示主线程被 [CPTAxis layoutSublayers][CPTLayer drawInContext] 阻塞。

延迟不是由于数据集过大:它目前正好包含两个点。该图非常简单,如下所示:

enter image description here

View Controller 的完整实现:​​

界面:

//
//  ICCaloriesGraphController.h
//  iCalories
//
//  Created by David Fearon on 22/08/2013.
//  Copyright (c) 2013 David Fearon. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
#import "ICDatabaseBrain.h"
#import "Day.h"
#import "CalorieEntry.h"

@interface ICCaloriesGraphController : UIViewController<CPTPlotDataSource, NSFetchedResultsControllerDelegate>
@property (weak, nonatomic) IBOutlet CPTGraphHostingView *graphHostingView;
@property (nonatomic, retain)ICDatabaseBrain* sharedDatabaseBrain;
@property (nonatomic, retain)NSFetchedResultsController* resultsController;

@end

实现:

//
//  ICCaloriesGraphController.m
//  iCalories
//
//  Created by David Fearon on 22/08/2013.
//  Copyright (c) 2013 David Fearon. All rights reserved.
//

#import "ICCaloriesGraphController.h"

@interface ICCaloriesGraphController ()

@property int numberOfRecordsForPlot;
@property float maxDailyCaloriesInDataset;
@property NSArray* days;

@end

@implementation ICCaloriesGraphController

- (id)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.sharedDatabaseBrain = [ICDatabaseBrain sharedDatabaseBrain];
    self.resultsController = self.sharedDatabaseBrain.fetchDaysResultsController;

    self.days = [self.resultsController fetchedObjects];
    self.numberOfRecordsForPlot = [self.days count];

    [self updateMaxDailyCaloriesInDataset];

    // Create a CPTGraph object and add to hostView
    CPTGraph* graph = [[CPTXYGraph alloc] initWithFrame:self.graphHostingView.bounds];
    self.graphHostingView.hostedGraph = graph;

    // Get the (default) plotspace from the graph so we can set its x/y ranges
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) self.graphHostingView.hostedGraph.defaultPlotSpace;

    // Note that these CPTPlotRange are defined by START and LENGTH (not START and END) !!
    [plotSpace setYRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat( self.maxDailyCaloriesInDataset )]];
    [plotSpace setXRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat( self.numberOfRecordsForPlot )]];

    // Create the plot (we do not define actual x/y values yet, these will be supplied by the datasource...)
    CPTScatterPlot* plot = [[CPTScatterPlot alloc] initWithFrame:CGRectZero];

    // Let's keep it simple and let this class act as datasource (therefore we implement <CPTPlotDataSource>)
    plot.dataSource = self;

    // Finally, add the created plot to the default plot space of the CPTGraph object we created before
    [self.graphHostingView.hostedGraph addPlot:plot toPlotSpace:self.graphHostingView.hostedGraph.defaultPlotSpace];

}

-(void)viewWillAppearBOOL)animated{

    [super viewWillAppear:animated];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSUInteger)numberOfRecordsForPlotCPTPlot *)plotnumberOfRecords {
    return self.numberOfRecordsForPlot;
}


-(void)updateMaxDailyCaloriesInDataset{

    if (self.numberOfRecordsForPlot == 0){
        self.maxDailyCaloriesInDataset = 0;
        return;
    }

    int max = 0;

    for (Day* day in self.days) {

        //count the calories in the day:
        int calorieSum = 0;
        for (CalorieEntry* calorieEntry in [day.calorieEntries allObjects]) {
            calorieSum += calorieEntry.calories.intValue;
        }

        if(calorieSum > max) max = calorieSum;

    }

    self.maxDailyCaloriesInDataset = max;

}


-(NSNumber *)numberForPlotCPTPlot *)plot fieldNSUInteger)fieldEnum recordIndexNSUInteger)index
{

    if (self.numberOfRecordsForPlot == 0){
        return 0;
    }

    if(fieldEnum == CPTScatterPlotFieldY){

        Day* day = [self.days objectAtIndex:index];

        //count the calories in the day:
        int calorieSum = 0;
        for (CalorieEntry* calorieEntry in [day.calorieEntries allObjects]) {
            calorieSum += calorieEntry.calories.intValue;
        }

        return [NSNumber numberWithInt:calorieSum];
        //TODO: Set range for y axis

    }

    if(fieldEnum == CPTScatterPlotFieldX){
        return [NSNumber numberWithInt: index];
    }else{
        NSLog(@"fieldEnum was neither CPTScatterPlotFieldY or CPTScatterPlotFieldX in numberForPlot:");
        return 0;
    }

}

@end

延迟绝对与 Core Data 代码无关;一切正常。谷歌搜索只会显示大型数据集的问题,而不是只有两点的问题。显然,我对核心情节代码做了一些根本性的错误,但真的看不出是什么。

请问有人能发现问题吗?



Best Answer-推荐答案


maxDailyCaloriesInDataset 大吗?默认轴标签策略将刻度标记和标签沿轴分开一个单位。如果 yRange 很大,这会创建很多重叠的标签,这可能会非常慢。根据您的应用程序的要求,增加 majorIntervalLength 使其不会制作这么多标签,或者更改标签策略。 CPTAxisLabelingPolicyAutomaticCPTAxisLabelingPolicyEqualDivisions 会是不错的选择。

关于ios - 加载包含 Core Plot 图的 View Controller 时延迟过大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18404887/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4