corePlot提示及iOS記憶體最佳化之道

來源:互聯網
上載者:User

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


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.