您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

CorePlot 使用技巧與 iOS 記憶體優化實務

編輯:iOS 開發綜合

本文整理 CorePlot 繪圖的實用技巧,並歸納一套 iOS 記憶體優化與洩漏排查的經驗法則。

CorePlot 使用技巧

設定內邊距:

graph.plotAreaFrame.paddingLeft   +=5;

graph.plotAreaFrame.paddingTop    +=5;

graph.plotAreaFrame.paddingRight  +=5;

graph.plotAreaFrame.paddingBottom +=17.5;

禁止縮放(兩指捏合):

[self setAllowPinchScaling:NO];

讓座標只能沿 X 軸橫向滑動:

plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromFloat(1)];

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

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

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

自訂座標軸 label 時,需把 labelingPolicy 設為 CPTAxisLabelingPolicyNone,此後座標軸 label 與背景刻度線都得自行繪製,否則會顯示為空,不必驚慌:

x.axisLabels          = [self buildLabelTitle];

x.labelingPolicy      = CPTAxisLabelingPolicyNone;

x.minorTickLocations  = [NSSet setWithArray:locationLabels];

自訂 label 的建構方法:

- (NSMutableSet*)buildLabelTitle

{

    NSMutableSet *newAxisLabels = [NSMutableSet set];

    CPTMutableTextStyle *textStyleB = [CPTMutableTextStyle textStyle];

    textStyleB.color = [CPTColor colorWithComponentRed: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 = [[CPTAxisLabel alloc] 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];

在組織與使用資料源時,這裡刻意記錄 MAX_data 的原因,是因為最好使用真實資料的相對值;否則當數值很大時會拖慢 CorePlot 效能,導致圖形載入緩慢。

另外,用延遲函式去呼叫資料初始化能提高載入速度:

[self performSelector:@selector(initPlotData) withObject:nil afterDelay:0.2];

iOS 記憶體優化與排查

1. IBOutlet 物件需要 release。

2. 不斷往 UIView(尤其是 UIScrollView)上 add 相同 SubView 時,一定要記得先清除舊的 SubView,並在 dealloc 中執行:

for (UIView* sbView in scrvBg.subviews)

{

    [sbView removeFromSuperview];

}

取得 subView 的小技巧:先用 [subView setTag:300] 標記,再用 [self.view viewWithTag:300] 取回。

3. dealloc 不一定會被呼叫,因此可自寫一個 myRelease 方法,在離開該介面時手動釋放需要釋放的物件並置為 nil。

4. 若不太清楚 drawRect 的呼叫時機,千萬不要輕易在其中寫入程式碼,特別是那些沒有立即 release 的物件,很容易因多次呼叫 drawRect 而造成記憶體溢出。

5. 檢查記憶體洩漏最好的工具是 Xcode,但它只能查出明顯的程式碼級洩漏,像第 4 點那樣因多次呼叫某函式卻未配對 release 的邏輯性洩漏則排查不出,只能靠閱讀程式碼。經驗法則是:alloc 的物件應立即 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