2010-09-01 85 views
0

即时通讯新的iPhone开发,所以请教我的东西,我可能会错过。 所以基本上我有graphview.m在这种方式核心动画:无法分配字节。使用核心在iPad上的阴谋

CorePlotViewController *aCorePlotViewController = [[CorePlotViewController alloc] initWithNibName:@"CorePlotViewController" bundle:nil]; 
aCorePlotViewController.view.bounds = CGRectMake(0,0,896,605); 
aCorePlotViewController.view.center = CGPointMake(576, 374.5); 
[aCorePlotViewController.view setTag:99]; 

[self.view addSubview:aCorePlotViewController.view]; 

我有一个graphview.m按钮,将排序刷新Coreplotview以这种方式

[[self.view viewWithTag:99]removeFromSuperview]; 

CorePlotViewController *aCPView = [[CorePlotViewController alloc] initWithNibName:@"CorePlotViewController" bundle:nil]; 
aCPView.view.bounds = CGRectMake(0,0,896,605); 
aCPView.view.center = CGPointMake(576, 374.5); 
[aCPView.view setTag:99]; 
[self.view addSubview:aCPView.view]; 

创建coreplotviewcontroller.view模拟器工作正常,具有图形的coreplot视图正在刷新。但在iPad上刷新一段时间后,出现如下错误:

CoreAnimation:未能分配2168448字节。

所以即时思考也许分配给它的内存没有发布?(我不知道)。

这是我的CorePlotViewController.h和.m文件。

CorePlotViewController.h

#import <UIKit/UIKit.h> 
#import "CorePlot-CocoaTouch.h" 
#import "GraphView.h" 

@interface CorePlotViewController : UIViewController <CPPlotDataSource> 

{ 
CPXYGraph *graph; 

NSMutableArray *dataForPlot; 
NSMutableArray *dataForPlot2; 

NSMutableArray *finalDatas; 

NSMutableArray *numofdata; 
NSMutableArray *numofdata2; 
} 

@property(readwrite, retain, nonatomic) NSMutableArray *dataForPlot; 
@property(readwrite, retain, nonatomic) NSMutableArray *dataForPlot2; 
@property(readwrite, retain, nonatomic) NSMutableArray *finalDatas; 

@property(readwrite, retain, nonatomic) NSMutableArray *numofdata; 
@property(readwrite, retain, nonatomic) NSMutableArray *numofdata2; 

@end 

CorePlotViewController.m

#import "MedicalBedAppDelegate.h" 
#import "CorePlotViewController.h" 

#import "UsageData.h" 
#import "GraphView.h" 

@implementation CorePlotViewController 

@synthesize dataForPlot; 
@synthesize dataForPlot2; 
@synthesize finalDatas; 
@synthesize numofdata,numofdata2; 

-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot{ 

    MedicalBedAppDelegate *appDelegate = (MedicalBedAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    if ([appDelegate.timetodisplay objectAtIndex:0] == @"4 Hours") { 
     return appDelegate.usagedatas.count; 
    } 
    else if ([appDelegate.timetodisplay objectAtIndex:0] == @"1 Hour") { 
     return appDelegate.usagedatas.count; 
    } 

    else if ([appDelegate.timetodisplay objectAtIndex:0] == @"8 Minutes") 
    { 
     return appDelegate.usagedatas.count; 
    } 
    else if ([appDelegate.timetodisplay objectAtIndex:0] == @"1 Minute") { 
     return appDelegate.usagedatas.count; 
    } 

} 


- (void)viewDidLoad { 

    [super viewDidLoad]; 


MedicalBedAppDelegate *appDelegate = (MedicalBedAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    graph = [[CPXYGraph alloc] initWithFrame: self.view.bounds]; 
    self.view = [[CPLayerHostingView alloc]initWithFrame:[UIScreen mainScreen].bounds]; 
    CPLayerHostingView *hostingView = (CPLayerHostingView *)self.view; 
    hostingView.hostedLayer = graph; 
    graph.paddingLeft = 0.0; 
    graph.paddingTop = 0.0; 
    graph.paddingRight = 0.0; 
    graph.paddingBottom = 0.0; 

    //This applies the 'background color to the graph available themes are 
    //kCPDarkGradientTheme,kCPPlainWhiteTheme,KCPPlainBlackTheme,kCPStocksTheme,kCPSlateTheme 
    CPTheme *Theme =[CPTheme themeNamed:kCPSlateTheme]; 
    [graph applyTheme:Theme]; 


    CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace; 
    plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-0.015) length:CPDecimalFromFloat(14.2)]; 
    //plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-0.5) length:CPDecimalFromFloat(14.4)]; 
    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-0.015) length:CPDecimalFromFloat(19.85)]; 
    //plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-1) length:CPDecimalFromFloat(19.85)]; 

    CPLineStyle *lineStyle = [CPLineStyle lineStyle]; 
    lineStyle.lineColor = [CPColor blackColor]; 
    lineStyle.lineWidth = 0.0f; 


    CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet; 
    axisSet.xAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"1"] decimalValue]; 
    //axisSet.xAxis.majorIntervalLength = CPDecimalFromFloat(1.0); 
    axisSet.xAxis.minorTicksPerInterval = 9; 
    axisSet.xAxis.majorTickLineStyle = lineStyle; 
    axisSet.xAxis.minorTickLineStyle = lineStyle; 
    axisSet.xAxis.axisLineStyle = lineStyle; 
    axisSet.xAxis.minorTickLength = 5.0f; 
    axisSet.xAxis.majorTickLength = 10.0f; 
    axisSet.xAxis.labelOffset = 5.0f; 
    //axisSet.xAxis.majorTickLocations = 2; 


    axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"1"] decimalValue]; 
    axisSet.yAxis.minorTicksPerInterval = 9; 
    axisSet.yAxis.majorTickLineStyle = lineStyle; 
    axisSet.yAxis.minorTickLineStyle = lineStyle; 
    axisSet.yAxis.axisLineStyle = lineStyle; 
    axisSet.yAxis.minorTickLength = 0.0f; 
    axisSet.yAxis.majorTickLength = 0.0f; 
    axisSet.yAxis.labelOffset = 5.0f; 


    //Alloc and init a Scatter plot 
    CPScatterPlot *breathRatePlot = [[CPScatterPlot alloc]initWithFrame:CGRectMake(90, 12, 200, 25)] ; 
    breathRatePlot.identifier = @"Breath Rate Plot"; 
    breathRatePlot.dataLineStyle.lineWidth = 3.3f; 
    breathRatePlot.dataLineStyle.lineColor = [CPColor yellowColor]; 
    breathRatePlot.dataSource = self; 
    [graph addPlot:breathRatePlot]; 
    //Add Plot symbols to the points 
    CPPlotSymbol *greenCirclePlotSymbol = [CPPlotSymbol ellipsePlotSymbol]; 
    greenCirclePlotSymbol.fill = [CPFill fillWithColor:[CPColor greenColor]]; 
    greenCirclePlotSymbol.size = CGSizeMake(1.0 , 1.0); 
    breathRatePlot.plotSymbol = greenCirclePlotSymbol; 


    //Alloc and init a Scatter plot 
    CPScatterPlot *noOfMovementRegisteredPlot = [[CPScatterPlot alloc]initWithFrame:CGRectMake(90, 12, 200, 25)]; 
    noOfMovementRegisteredPlot.identifier = @"Move Registered Plot"; 
    noOfMovementRegisteredPlot.dataLineStyle.lineWidth = 2.3f; 
    noOfMovementRegisteredPlot.dataLineStyle.lineColor = [CPColor redColor]; 
    noOfMovementRegisteredPlot.dataSource = self; 
    [graph addPlot:noOfMovementRegisteredPlot]; 
    //Add Plot symbols to the points 
    CPPlotSymbol *greenCirclePlotSymbol2 = [CPPlotSymbol ellipsePlotSymbol]; 
    greenCirclePlotSymbol2.fill = [CPFill fillWithColor:[CPColor greenColor]]; 
    greenCirclePlotSymbol2.size = CGSizeMake(1.0, 1.0); 
    noOfMovementRegisteredPlot.plotSymbol = greenCirclePlotSymbol2; 



    //Get an instance of the appDelegate and set some vars to be used 
    NSInteger indexPRowArrayInteger; 
    NSString *indexPRowArrayString; 
    indexPRowArrayString = [appDelegate.indexPathRowArray objectAtIndex:0 ] ; 
    indexPRowArrayInteger = [indexPRowArrayString integerValue ]; 

    // Add some initial data 
    NSMutableArray *contentArray =[[NSMutableArray alloc]init]; 
    NSMutableArray *contentArray2 =[[NSMutableArray alloc]init]; 

    NSNumber *BRNumber; 
    NSString *BRString; 
    NSNumber *NOMRNumber; 
    NSString *NOMRString; 
    NSString *DString; 


    NSNumberFormatter *f = [[NSNumberFormatter alloc]init]; 
    [f setNumberStyle:NSNumberFormatterDecimalStyle]; 

    //Datetime section 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"dd/MM/yyyy"]; 

    NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; 
    [timeFormat setDateFormat:@"HH:mm:ss"]; 
    NSDate *now = [[NSDate alloc] initWithTimeIntervalSinceNow:-4*60*60];//time from 4 hrs ago 
    NSString *theTime = [timeFormat stringFromDate:now]; 
    NSDate *cDate1 = [timeFormat dateFromString:theTime]; 

    NSDate *now1Hr = [[NSDate alloc] initWithTimeIntervalSinceNow:-1*60*60];//time from 1hr ago 
    NSString *theTime1Hr = [timeFormat stringFromDate:now1Hr]; 
    NSDate *cDate1Hr = [timeFormat dateFromString:theTime1Hr]; 

    NSDate *now8Mins = [[NSDate alloc] initWithTimeIntervalSinceNow:-8*60];//time from 8Mins ago 
    NSString *theTime8Mins = [timeFormat stringFromDate:now8Mins]; 
    NSDate *cDate8Mins = [timeFormat dateFromString:theTime8Mins]; 

    NSDate *now1Min = [[NSDate alloc] initWithTimeIntervalSinceNow:-1*60];//time from 1Min ago 
    NSString *theTime1Min = [timeFormat stringFromDate:now1Min]; 
    NSDate *cDate1Min = [timeFormat dateFromString:theTime1Min]; 

    NSUInteger i; 
    NSUInteger i2; 
    numofdata = [[NSMutableArray alloc]init]; 
numofdata2 = [[NSMutableArray alloc]init]; 

for (i = 0; i < appDelegate.usagedatas.count; i++) { 
    //NSLog(@"appdelcount= %d",appDelegate.usagedatas.count); 

    UsageData *aUsageDataInstance = (UsageData *)[appDelegate.usagedatas objectAtIndex:i]; 

    BRString = aUsageDataInstance.breathrate; 
    BRNumber = [f numberFromString:BRString]; 

    DString = aUsageDataInstance.time; 
    NSDate *cDate2 = [timeFormat dateFromString:DString]; 

    NSTimeInterval time = [cDate2 timeIntervalSinceDate:cDate1]; 
    NSTimeInterval time1Hr = [cDate2 timeIntervalSinceDate:cDate1Hr]; 
    NSTimeInterval time8Mins = [cDate2 timeIntervalSinceDate:cDate8Mins]; 
    NSTimeInterval time1Min = [cDate2 timeIntervalSinceDate:cDate1Min]; 

    if ([appDelegate.timetodisplay objectAtIndex:0] == @"4 Hours") { 
     id x = [NSNumber numberWithFloat: ((time/132)/9)]; 
     id y = [NSNumber numberWithFloat:([BRNumber intValue]/1.975) ]; 
     [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]]; 
    } 


     //calculation = (time(interval from how many hrs/mins ago)/seconds of 1 minor-interval of x-axis(NOT MAJOR,1major has 10 minor intervals)/9 
      //time = the number of seconds from the time in the database to the current time 

     else if ([appDelegate.timetodisplay objectAtIndex:0] == @"1 Hour") { 
      id x = [NSNumber numberWithFloat:((time1Hr/33.3)/9)]; 
      id y = [NSNumber numberWithFloat:([BRNumber intValue]/1.975) ]; 
      [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]]; 
     } 
     else if ([appDelegate.timetodisplay objectAtIndex:0] == @"8 Minutes") { 
      id x = [NSNumber numberWithFloat: ((time8Mins/4.4)/9)]; 
      id y = [NSNumber numberWithFloat:([BRNumber intValue]/1.975) ]; 
      [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]]; 
     } 
     else if ([appDelegate.timetodisplay objectAtIndex:0] == @"1 Minute") { 
      id x = [NSNumber numberWithFloat: ((time1Min/0.53)/9)]; 
      id y = [NSNumber numberWithFloat:([BRNumber intValue]/1.975) ]; 
      [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]]; 
     } 
    } 
    self.dataForPlot = contentArray; 



    for (i2 = 0; i2 < appDelegate.usagedatas.count; i2++) { 
     UsageData *aUsageDataInstance = (UsageData *)[appDelegate.usagedatas objectAtIndex:i2]; 

     NOMRString = aUsageDataInstance.noofmovereg; 
     NOMRNumber = [f numberFromString:NOMRString]; 

     DString = aUsageDataInstance.time;  
     NSDate *cDate2 = [timeFormat dateFromString:DString]; 

     NSTimeInterval time = [cDate2 timeIntervalSinceDate:cDate1]; 
     NSTimeInterval time1Hr = [cDate2 timeIntervalSinceDate:cDate1Hr]; 
     NSTimeInterval time8Mins = [cDate2 timeIntervalSinceDate:cDate8Mins]; 
     NSTimeInterval time1Min = [cDate2 timeIntervalSinceDate:cDate1Min]; 

     if ([appDelegate.timetodisplay objectAtIndex:0] == @"4 Hours") { 
     id x2 = [NSNumber numberWithFloat: ((time/132)/9)]; 
     id y2 = NOMRNumber; 
     [contentArray2 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x2, @"x", y2, @"y", nil]]; 

     } 
    else if ([appDelegate.timetodisplay objectAtIndex:0] == @"1 Hour") { 
      id x2 = [NSNumber numberWithFloat:((time1Hr/33.3)/9)]; 
      id y2 = NOMRNumber; 
      [contentArray2 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x2, @"x", y2, @"y", nil]]; 

     } 
     else if ([appDelegate.timetodisplay objectAtIndex:0] == @"8 Minutes") { 
      id x2 = [NSNumber numberWithFloat: ((time8Mins/4.4)/9)]; 
      id y2 = NOMRNumber; 
      [contentArray2 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x2, @"x", y2, @"y", nil]]; 

     } 
     else if ([appDelegate.timetodisplay objectAtIndex:0] == @"1 Minute") { 
      id x2 = [NSNumber numberWithFloat: ((time1Min/0.53)/9)]; 
      id y2 = NOMRNumber; 
      [contentArray2 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x2, @"x", y2, @"y", nil]]; 
     } 
    } 
    self.dataForPlot2 = contentArray2; 

    //release for memory management. 
    [dateFormat release]; 
    [timeFormat release]; 
    [now release]; 
    [now1Hr release]; 
    [now8Mins release]; 
    [now1Min release]; 
    [f release]; 
    [noOfMovementRegisteredPlot release]; 
    [breathRatePlot release]; 

    [contentArray release]; 
    [contentArray2 release]; 

    } 

-(NSNumber *)numberForPlot:(CPPlot *)plot 
        field:(NSUInteger)fieldEnum 
       recordIndex:(NSUInteger)index 
{ 
    NSNumber *num = [[dataForPlot objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")]; 
    NSNumber *num2 = [[dataForPlot2 objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")]; 

    if ([(NSString *)plot.identifier isEqualToString:@"Breath Rate Plot"]) 
    { 
     if (fieldEnum == CPScatterPlotFieldY) 
      num = [NSNumber numberWithDouble:[num doubleValue] ]; 
     return num; 
    } 
     else { 
      num2 = [NSNumber numberWithDouble:[num2 doubleValue] ]; 
     return num2; 
    } 


} 
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft); 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)dealloc { 


    [graph release]; 
    [dataForPlot release]; 
    [dataForPlot2 release]; 
    [finalDatas release]; 
    [numofdata release]; 
    [numofdata2 release]; 
    [super dealloc]; 

} 

@end 

回答

3

你不释放或者您的CorePlotViewController情况下,过段时间会引起问题的。将VC视图添加为子视图后,您可以释放控制器,即[aCorePlotViewController release];。然后为刷新做同样的事情。

如果这样不能解决问题,请尝试执行构建&分析其他位置是否存在内存问题。

+0

没有帮助。 :( – Kenneth 2010-09-02 05:25:32

+0

你是否做了构建和分析?如果你做了并修复了所有问题,那么问题在别处。这是一个内存问题,因为只有当你的应用程序使用太多内存时才会出现该错误。 – MishieMoo 2010-09-02 13:27:09

+0

想知道是否你遇到了这个问题的底部?我遇到了与CoreText相似的问题,我的构建和分析没有问题,泄漏也没有显示任何泄漏,所以很容易混淆 – Arun 2010-10-01 02:46:45