美好的一天,我有一个显示单个散点图的应用程序。一切都有效,但绘制散点图时会增加内存。数据来自.txt文件,转换为Mutable数组并显示为散点图。我测试了数组,他们应该填充它们。 应用程序在显示绘图之前加载显示在表视图中的文本文件中的数据。 有人可以帮助我找出为什么情节不再需要工作以及为什么内存使用量不断增加。
tableView选择:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DataSource *sharedManager = [DataSource sharedManager];
NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectoryPath error:NULL];
NSString *filePath = [documentDirectoryPath stringByAppendingPathComponent:[directoryContent objectAtIndex:indexPath.row]];
sharedManager.selectedFile = [NSString stringWithContentsOfFile:filePath encoding:NSASCIIStringEncoding error:NULL];
sharedManager.tappedLine = indexPath.row;// not +1
[self populateArraysWithFileData:^(BOOL finished){
if (finished) {
NSLog(@"success");
[self performSegueWithIdentifier:@"graph" sender:nil]; //view controllers are linked
}
}];
}
这是graph.m文件:
#import "CorePlot-CocoaTouch.h"
#import "GraphViewController.h"
#import "DataSource.h"
#import "methods.h"
@interface GraphViewController ()
@end
@implementation GraphViewController
NSString * const CPDTickerSymbol = @"STAR";
@synthesize hostView = hostView_;
#pragma mark - UIViewController lifecycle methods
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self initPlot];
DataSource *sharedManager = [DataSource sharedManager];
NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectoryPath error:NULL];
// 2 - Set graph title
NSString *title = [NSString stringWithFormat:@"%@",[directoryContent objectAtIndex:sharedManager.tappedLine]];
[self.navigationItem setTitle:title];
sharedManager.labelArray = nil;
[self initPlot];
}
- (NSArray *)tickerSymbols
{
static NSArray *symbols = nil;
if (!symbols)
{
symbols = [NSArray arrayWithObjects:
@"STAR",
nil];
}
return symbols;
}
#pragma mark - Chart behavior
-(void)initPlot {
[self configureHost];
[self configureGraph];
[self configurePlots];
[self configureAxes];
}
-(void)configureHost {
self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
self.hostView.allowPinchScaling = YES;
[self.view addSubview:self.hostView];
}
-(void)configureGraph {
DataSource *sharedManager = [DataSource sharedManager];
// 1 - Create the graph
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
[graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
self.hostView.hostedGraph = graph;
NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectoryPath error:NULL];
// 2 - Set graph title
NSString *title = [NSString stringWithFormat:@"%@",[directoryContent objectAtIndex:sharedManager.tappedLine]];
graph.title = title;
// 3 - Create and set text style
CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor whiteColor];
titleStyle.fontName = @"Helvetica-Bold";
titleStyle.fontSize = 16.0f;
graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(20.0f, 0.0f);
// 4 - Set padding for plot area
[graph.plotAreaFrame setPaddingLeft:40.0f];
[graph.plotAreaFrame setPaddingBottom:10.0f];
// 5 - Enable user interactions for plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
}
-(void)configurePlots {
// 1 - Get graph and plot space
CPTGraph *graph = self.hostView.hostedGraph;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
// 2 - Create the plot
CPTScatterPlot *magPlot = [[CPTScatterPlot alloc] init];
magPlot.dataSource = self;
// Make the plot curved
magPlot.interpolation = CPTScatterPlotInterpolationCurved;
magPlot.identifier = CPDTickerSymbol;
CPTColor *magColor = [CPTColor redColor];
[graph addPlot:magPlot toPlotSpace:plotSpace];
// 3 - Set up plot space
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:magPlot, nil]];
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:@1.1];
plotSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:@1.2];
plotSpace.yRange = yRange;
// 4 - Create styles and symbols
CPTMutableLineStyle *magLineStyle = [magPlot.dataLineStyle mutableCopy];
magLineStyle.lineWidth = 1.0;
magLineStyle.lineColor = magColor;
magPlot.dataLineStyle = magLineStyle;
CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle];
symbolLineStyle.lineColor = magColor;
CPTPlotSymbol *symbol = [CPTPlotSymbol starPlotSymbol];
symbol.fill = [CPTFill fillWithColor:magColor];
symbol.lineStyle = symbolLineStyle;
symbol.size = CGSizeMake(6.0f, 6.0f);
magPlot.plotSymbol = symbol;
}
-(void)configureAxes {
// Calculate the increment values for the y-axis
DataSource *sharedManager = [DataSource sharedManager];
float ymax = -MAXFLOAT;
float ymin = MAXFLOAT;
for (NSNumber *num in sharedManager.finalFIELDreading) {
float y = num.floatValue;
if (y < ymin) ymin = y;
if (y > ymax) ymax = y;
}
int MinFieldReading = roundf(ymin);
int MaxFieldReading = roundf(ymax);
int diffFieldReading = MaxFieldReading-MinFieldReading;
int Increment = diffFieldReading/[sharedManager.finalFIELDreading count];
int incrementValue = 10 * floor( Increment / 10 + 0.5 );
// 1 - Create styles
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = @"Helvetica-Bold";
axisTitleStyle.fontSize = 12.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor whiteColor];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = @"Helvetica-Bold";
axisTextStyle.fontSize = 11.0f;
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor whiteColor];
tickLineStyle.lineWidth = 2.0f;
CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor blackColor];
tickLineStyle.lineWidth = 1.0f;
// 2 - Get axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
// 3 - Configure x-axis
NSArray *yMinNumbers = [sharedManager.finalFIELDreading sortedArrayUsingSelector:@selector(compare:)];// should determine dynamically based on min reading
CGFloat yMin = [yMinNumbers[0] floatValue];
// Set Axis to start just below the min y value
axisSet.xAxis.orthogonalPosition = @(yMin);
CPTAxis *x = axisSet.xAxis;
x.title = @"Station (m)";
x.titleTextStyle = axisTitleStyle;
x.titleOffset = 15.0f;
x.axisLineStyle = axisLineStyle;
x.majorGridLineStyle = gridLineStyle;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.labelTextStyle = axisTextStyle;
x.majorTickLineStyle = axisLineStyle;
x.majorTickLength = 4.0f;
x.tickDirection = CPTSignNegative;
CGFloat stationCount = [sharedManager.finalSTNreading count];
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:stationCount];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:stationCount];
NSInteger i = 0;
for (NSString *station in sharedManager.finalSTNreading) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:station textStyle:x.labelTextStyle];
CGFloat location = i++;
label.tickLocation = @(location);
label.offset = x.majorTickLength;
if (label) {
[xLabels addObject:label];
[xLocations addObject:[NSNumber numberWithFloat:location]];
}
}
x.axisLabels = xLabels;
x.majorTickLocations = xLocations;
// 4 - Configure y-axis
CPTAxis *y = axisSet.yAxis;
y.title = @"Field Reading (nTesla)";
y.titleTextStyle = axisTitleStyle;
y.titleOffset = 37.0f;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = gridLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.labelOffset = -7.0f;
y.majorTickLineStyle = axisLineStyle;
y.majorTickLength = 4.0f;
y.tickDirection = CPTSignNegative;
NSInteger majorIncrement = incrementValue;
NSArray *yMaxNumbers = [sharedManager.finalFIELDreading sortedArrayUsingSelector:@selector(compare:)];// should determine dynamically based on max reading
CGFloat yMax = [[yMaxNumbers lastObject] floatValue];
NSMutableSet *yLabels = [NSMutableSet set];
NSMutableSet *yMajorLocations = [NSMutableSet set];
for (NSInteger j = yMin; j <= (yMax+majorIncrement); j += majorIncrement) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%li", (long)j] textStyle:y.labelTextStyle];
NSNumber *location = @(j);
label.tickLocation = location;
label.offset = -y.majorTickLength - y.labelOffset;
if (label) {
[yLabels addObject:label];
}
[yMajorLocations addObject:location];
}
y.axisLabels = yLabels;
y.majorTickLocations = yMajorLocations;
}
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index {
DataSource *sharedManager = [DataSource sharedManager];
CPTTextLayer *newTextLayer;
newTextLayer = [CPTTextLayer layer];
NSNumber* num = [[NSNumber alloc] init];
if(index < [sharedManager.labelArray count])
{
num = [NSNumber numberWithFloat:[[sharedManager.labelArray objectAtIndex:index] floatValue]];
CPTMutableTextStyle *labelTextStyle = [CPTMutableTextStyle textStyle];
labelTextStyle.fontSize = 10.0f;
labelTextStyle.color = [CPTColor redColor];
newTextLayer.textStyle = labelTextStyle;
newTextLayer.text = [NSString stringWithFormat:@"%@%i%@",@"(", [num intValue],@")"];
}
return newTextLayer;
}
- (void)viewDidLoad{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Returns the number of stations in the line based on the data source. Since the chart will have a plot point for each field reading, this defines the number of records for each plot.
#pragma mark - CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return [[[DataSource sharedManager] finalFIELDreading]count];
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
DataSource *sharedManager = [DataSource sharedManager];
NSInteger valueCount = [sharedManager.finalSTNreading count];
switch (fieldEnum) {
case CPTScatterPlotFieldX:
if (index < valueCount) {
return [NSNumber numberWithUnsignedInteger:index];
}
break;
case CPTScatterPlotFieldY:
if ([plot.identifier isEqual:CPDTickerSymbol] == YES) {
return [sharedManager.finalFIELDreading objectAtIndex:index];
return [sharedManager.finalSTNreading objectAtIndex:index];
}
break;
}
return [NSDecimalNumber zero];
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self initPlot];
}
这是为图表创建数组的方法,我将其设置为本地和块:
-(void)populateArraysWithFileData:(arrayLoadCompletion) compblock
{
DataSource *sharedManager = [DataSource sharedManager];
[sharedManager.finalSTNreading removeAllObjects];
[sharedManager.finalLINEreading removeAllObjects];
[sharedManager.finalDAYreading removeAllObjects];
[sharedManager.finalTIMEreading removeAllObjects];
[sharedManager.finalFIELDreading removeAllObjects];
[sharedManager.finalSreading removeAllObjects];
[sharedManager.finalNreading removeAllObjects];
sharedManager.FnumbersArray = nil;
sharedManager.didFinishArrays = 0;
NSUInteger finalCount = 0;
// make a local string from the selected file data string
NSString *text = @"";
text = [sharedManager.selectedFile stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//NSLog(@"%@", text);
//[text cStringUsingEncoding:NSUTF8StringEncoding];
// Remove newlines
while ([text rangeOfString:@"\n"].location != NSNotFound)
text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
while ([text rangeOfString:@"\t"].location != NSNotFound)
text = [text stringByReplacingOccurrencesOfString:@"\t" withString:@" "];
// NSLog(@"%@", text);
NSString *numbersonly = [[text componentsSeparatedByString:@" N "] objectAtIndex:1];
//NSLog(@"%@", numbersonly);
// Make an array with all of the formatted text seperated by a space
sharedManager.FnumbersArray = [numbersonly componentsSeparatedByString:@" "];
NSMutableArray * newDoubleArray = [[NSMutableArray alloc] initWithCapacity: [sharedManager.FnumbersArray count]];
for (NSString *string in sharedManager.FnumbersArray)
{
NSString *newString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// NSLog(@"%@",newString);
NSInteger trueDouble = [newString integerValue];
[newDoubleArray addObject:[NSNumber numberWithInteger:(trueDouble)]];
}
// NSLog(@"%@", newDoubleArray);
finalCount = [sharedManager.FnumbersArray count];
//NSLog(@"%lu", (unsigned long)finalCount);
//doubleValue or decimalValue
//Make an array with just the station numbers that will become the x-axis
for (int i = 0;i < finalCount;i += 7) {
[sharedManager.finalSTNreading addObject:[sharedManager.FnumbersArray objectAtIndex:i]]; //this does not like being an int
}
//Make an array with just the Line data, start at the second index in the numbers array and iterare through it
for (int i = 1;i < finalCount;i += 7) {
[sharedManager.finalLINEreading addObject:[sharedManager.FnumbersArray objectAtIndex:i]];
}
// NSLog(@"%@", sharedManager.STNreading);
//Make an array with just the day
for (int i = 2;i < finalCount;i += 7) {
[sharedManager.finalDAYreading addObject:[sharedManager.FnumbersArray objectAtIndex:i]];
}
//Make an array with just the Time
for (int i = 3;i < finalCount;i += 7) {
[sharedManager.finalTIMEreading addObject:[sharedManager.FnumbersArray objectAtIndex:i]];
}
//Make an array with just the field
for (int i = 4;i < finalCount;i += 7) {
[sharedManager.finalFIELDreading addObject:[newDoubleArray objectAtIndex:i]];
}
//Make an array with just the S
for (int i = 5;i < finalCount;i += 7) {
[sharedManager.finalSreading addObject:[sharedManager.FnumbersArray objectAtIndex:i]];
}
//Make an array with just the N
for (int i = 6;i < finalCount;i += 7) {
[sharedManager.finalNreading addObject:[sharedManager.FnumbersArray objectAtIndex:i]];
}
sharedManager.didFinishArrays = 1;
// NSLog(@"%@", sharedManager.finalSTNreading);
// NSLog(@"%@", sharedManager.finalLINEreading);
// NSLog(@"%@", sharedManager.FDAYreading);
// NSLog(@"%@", sharedManager.FTIMEreading);
// NSLog(@"%@", sharedManager.finalFIELDreading);
// NSLog(@"%@", sharedManager.FSreading);
// NSLog(@"%@", sharedManager.FNreading);
compblock(YES);
}
以下是块的日志输出和&#39; numberForPlot&#39; :
2016-08-02 14:20:28.425 GeoData Grapher[4262:258562] success
2016-08-02 14:20:28.438 GeoData Grapher[4262:258562] 0
2016-08-02 14:20:28.438 GeoData Grapher[4262:258562] 1
2016-08-02 14:20:28.438 GeoData Grapher[4262:258562] 2
2016-08-02 14:20:28.438 GeoData Grapher[4262:258562] 3
2016-08-02 14:20:28.439 GeoData Grapher[4262:258562] 4
2016-08-02 14:20:28.439 GeoData Grapher[4262:258562] 5
2016-08-02 14:20:28.439 GeoData Grapher[4262:258562] 6
2016-08-02 14:20:28.439 GeoData Grapher[4262:258562] 7
2016-08-02 14:20:28.439 GeoData Grapher[4262:258562] 8
2016-08-02 14:20:28.439 GeoData Grapher[4262:258562] 9
2016-08-02 14:20:28.440 GeoData Grapher[4262:258562] 10
2016-08-02 14:20:28.440 GeoData Grapher[4262:258562] 11
2016-08-02 14:20:28.440 GeoData Grapher[4262:258562] 12
2016-08-02 14:20:28.440 GeoData Grapher[4262:258562] 13
2016-08-02 14:20:28.440 GeoData Grapher[4262:258562] 14
2016-08-02 14:20:28.440 GeoData Grapher[4262:258562] 15
2016-08-02 14:20:28.441 GeoData Grapher[4262:258562] 16
2016-08-02 14:20:28.441 GeoData Grapher[4262:258562] 17
2016-08-02 14:20:28.441 GeoData Grapher[4262:258562] 27082
2016-08-02 14:20:28.442 GeoData Grapher[4262:258562] 27134
2016-08-02 14:20:28.442 GeoData Grapher[4262:258562] 27149
2016-08-02 14:20:28.442 GeoData Grapher[4262:258562] 27166
2016-08-02 14:20:28.442 GeoData Grapher[4262:258562] 27177
2016-08-02 14:20:28.442 GeoData Grapher[4262:258562] 27171
2016-08-02 14:20:28.442 GeoData Grapher[4262:258562] 27141
2016-08-02 14:20:28.443 GeoData Grapher[4262:258562] 27146
2016-08-02 14:20:28.443 GeoData Grapher[4262:258562] 27224
2016-08-02 14:20:28.443 GeoData Grapher[4262:258562] 27246
2016-08-02 14:20:28.443 GeoData Grapher[4262:258562] 27240
2016-08-02 14:20:28.443 GeoData Grapher[4262:258562] 27220
2016-08-02 14:20:28.443 GeoData Grapher[4262:258562] 27223
2016-08-02 14:20:28.443 GeoData Grapher[4262:258562] 27230
2016-08-02 14:20:28.444 GeoData Grapher[4262:258562] 27228
2016-08-02 14:20:28.444 GeoData Grapher[4262:258562] 27245
2016-08-02 14:20:28.444 GeoData Grapher[4262:258562] 27248
2016-08-02 14:20:28.444 GeoData Grapher[4262:258562] 27237