你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> corePlot使用技巧及iOS內存優化之道

corePlot使用技巧及iOS內存優化之道

編輯:關於IOS

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];                               ----------      正確

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved