现在的位置: 首页 > 综合 > 正文

CorePlot学习五—-corePlot使用技巧及iOS内存优化之道

2014年09月05日 ⁄ 综合 ⁄ 共 3527字 ⁄ 字号 评论关闭

corePlot 使用技巧:
设置内边距:
graph.plotAreaFrame.paddingLeft   +=5;

graph.plotAreaFrame.paddingTop    +=5;

graph.plotAreaFrame.paddingRight  +=5;

graph.plotAreaFrame.paddingBottom +=17.5;

 

禁止缩放:(两指捏扩动作)
[selfsetAllowPinchScaling:NO];//禁止缩放

 

设置坐标只能按照X轴横向滑动:(其他方向请自行理解)
plotSpace.yRange = [CPTPlotRangeplotRangeWithLocation:CPTDecimalFromInt(0)length:CPTDecimalFromFloat(1)];

plotSpace.globalYRange = [CPTPlotRangeplotRangeWithLocation:CPTDecimalFromInt(0)length:CPTDecimalFromFloat(1)];

plotSpace.xRange = [CPTPlotRangeplotRangeWithLocation:CPTDecimalFromInt(1)length:CPTDecimalFromFloat(1)];

plotSpace.globalXRange = [CPTPlotRangeplotRangeWithLocation:CPTDecimalFromInt(0)length:CPTDecimalFromFloat(2)];

 

自定义轴label:
x.axisLabels          = [self buildLabelTitle];

x.labelingPolicy      = CPTAxisLabelingPolicyNone;//当设置这个Policy之后,坐标轴label及背景线tick都需要自己绘制,否则显示为空,请不要过度惊慌

x.minorTickLocations  = [NSSetsetWithArray:locationLabels];

 

- (NSMutableSet*)buildLabelTitle

{

    NSMutableSet *newAxisLabels = [NSMutableSetset];

   

    CPTMutableTextStyle *textStyleB = [CPTMutableTextStyletextStyle];

    textStyleB.color = [CPTColorcolorWithComponentRed:CPTFloat((float)0x09/0xFF)green:CPTFloat((float)0x31/0xFF)blue:CPTFloat((float)0x4A/0xFF)alpha:CPTFloat(1.0)];

 

    int n = 1;

    for ( NSUInteger i =30; i > 0; i--)

    {

        CPTAxisLabel *newLabel = [[CPTAxisLabelalloc] initWithText:@“这里是内容”

                                                          textStyle:textStyleB];

        newLabel.tickLocation =CPTDecimalFromUnsignedInteger(n++);

        newLabel.offset = 5;

        [locationLabels addObject:[NSNumber numberWithFloat:(n-1) -0.25]];

        [locationLabels addObject:[NSNumber numberWithFloat:(n-1) +0.25]];

        [newAxisLabels addObject:newLabel];

        [newLabel release];

    }

    return newAxisLabels;

}

 

刷新图表内容:
[[bar1graph] reloadData];

 

组织数据源:
[m_SO2OnlineCaddObject:[NSMutableDictionarydictionaryWithObjectsAndKeys:x, @"x", y,@"y", nil]];

if (tmpY> MAX_data)

{

    MAX_data = tmpY;

}

 

使用数据源:
if ([(NSString*)plot.identifierisEqualToString:kBar1])

{

    switch (fieldEnum)

{

    caseCPTBarPlotFieldBarLocation:

        number = [[[m_SO2OnlineCobjectAtIndex:index] valueForKey:@"x"]doubleValue];

        break;

    caseCPTBarPlotFieldBarTip:

        number = [[[m_SO2OnlineCobjectAtIndex:index] valueForKey:@"y"]doubleValue]/ MAX_data;

        break;

    default:

        break;

    }

}

这里要记录MAX_data的原因是这里最好使用真实数据的相对数据,否则当数据值很大的时候会消耗corePlot的性能导致图形加载很慢。

 

用延迟函数去调用数据初始化可以提高加载速度:
[selfperformSelector:@selector(initPlotData)withObject:nilafterDelay:0.2];

 

计算日期的简单方法:
NSDateComponents* comps = [[NSDateComponentsalloc]init];

[comps setDay:-i];

NSDate *newDate = [[NSCalendarcurrentCalendar] dateByAddingComponents:compstoDate:[NSDate date]options:0];

--------------------------------------------------------

iOS内存优化及排查方法

1.IBOutlet 对象需要release

 

2.不停的往UIView,特别是UIScrollView上add相同SubView。一定要记得清除之前的SubView,并且在dealloc函数中执行该方法
for (UIView* sbViewin scrvBg.subviews)

{

    [sbView removeFromSuperview];

}

这里还有个获得subView的小技巧:

[subView setTag:300];

subView = [self.viewviewWithTag:300]

 

3.dealloc不一定会被调用,所以可以自己手写一个myRelease方法,当退出该界面的时候手动调用release需要释放的对象,并且将其置为nil。

 

4.记住,如果你不太明白UIView的drawRect的调用时机,千万不要轻易往drawRect里写代码,特别是没有立即release的对象。很容易在这里因为多次调用了drawRect而没有release该对象导致内存溢出。

 

5.检查内存泄漏最好的工具是xCode,当然不是说xCode工具排查完了就OK了。我们发现xCode只能检查明显的代码级别泄漏,而像上面第四点因为多次调用某个函数却没有配对release的逻辑性泄漏是排查不出来的,只能通过代码阅读排查。
我这里能给出的经验就是,alloc的对象应该立即release。如果该对象不能立即release,必须保证alloc和release必须配对调用,特别要留意那些可以多次调用且包含alloc却未被及时release的函数。四个字概括“非常危险”!

 

6.属性对象不要用Self.来alloc它,例如:
self.my_arr =[[NSArray alloc]init];    ----------     错误!

 

NSArray *tmpArr = [[NSArray alloc]init];

self.my = tmpArr;

[tmpArr release];                               ----------      正确

抱歉!评论已关闭.