您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

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

編輯:iOS 相關資訊

本文整理在專案中實際使用 Core Plot 繪製圖表時累積的實用技巧,並附上 iOS 記憶體優化的排查經驗。

一、Core Plot 常用技巧

設定內邊距

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)];

自訂座標軸標籤

x.axisLabels     = [self buildLabelTitle];
x.labelingPolicy = CPTAxisLabelingPolicyNone; // 設為此 Policy 後,label 與 tick 都需自行繪製
x.minorTickLocations = [NSSet setWithArray:locationLabels];

- (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];

使用資料源的關鍵技巧

case CPTBarPlotFieldBarTip:
    number = [[[m_SO2OnlineC objectAtIndex:index] valueForKey:@"y"] doubleValue] / MAX_data;
    break;

這裡之所以要記錄 MAX_data,是因為最好使用「真實數據的相對比例」來繪製。否則當數值很大時,會拖垮 Core Plot 的效能,導致圖形載入緩慢。

用延遲呼叫加速載入

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

簡易的日期計算

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:-i];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:[NSDate date] options:0];

二、iOS 記憶體優化與排查

  1. IBOutlet 物件需要 release。
  2. 避免不斷往 UIView(尤其 UIScrollView)addSubview 相同檢視。記得清除舊的子檢視,並在 dealloc 中執行:
    for (UIView *sbView in scrvBg.subviews) {
        [sbView removeFromSuperview];
    }
    
    取得子檢視的小技巧:[subView setTag:300]; subView = [self.view viewWithTag:300];
  3. dealloc 不一定會被呼叫。可自行撰寫 myRelease 方法,在離開畫面時手動釋放並將物件置為 nil。
  4. 慎寫 drawRect。若不清楚它的呼叫時機,千萬別隨意在裡面配置物件,否則多次觸發卻未對應 release,很容易造成記憶體溢出。
  5. 善用 Xcode 工具,但別全依賴它。Instruments/Analyze 能抓出明顯的程式碼級洩漏,但像「函式可被多次呼叫卻未配對 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