一、image使用相關(guān),見上一篇文章
二、 滑動列表的時候,使用UITableView的reuse機制
復(fù)制代碼
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
dequeueReusableCellWithIdentifier 方法會把隱藏的界面拿來重用,這樣節(jié)省很多資源。
三、頻繁打開和關(guān)閉SQLite,導(dǎo)致內(nèi)存不斷的增長
SQLite的數(shù)據(jù)庫本質(zhì)上來講就是一個磁盤上的文件,頻繁打開和關(guān)閉是很耗時和浪費資源的,可以設(shè)置SQLite的長連接方式;避免頻繁的打開和關(guān)閉數(shù)據(jù)庫;
四、在UITableView 的cellForRowAtIndexPath 代理中不要使用 stringWithFormat 方法
定義一個字符串變量有很多方法,最簡單的就是 NSString *str = @“abc”, 還有initWithString、stringWithFormat和stringWithCString等等。大量的字符操作時,不同的方法消耗不同的內(nèi)存。
[NSString stringWithString:@"abcc"]; 內(nèi)存增長過快
[NSString stringWithFormat:@"abcc%d",i]; 內(nèi)存增長過快
[[NSString alloc] initWithFormat:@"abcc%d",i]; 內(nèi)存稍有增長
NSMutableString *a=[[NSMutableString alloc] init];
[a stringByAppendingFormat:@"abcc%d",i];內(nèi)存增長過快
由于stringWithFormat 即耗時又耗內(nèi)存,所以在cellForRowAtIndexPath 繪制cell 的時消耗大量內(nèi)存和時間,造成界面滑動不流暢。
五. 在Image Views中調(diào)整圖片大小
如果要在UIImageView中顯示一個來自bundle的圖片,你應(yīng)保證圖片的大小和UIImageView的大小相同。在運行中縮放圖片是很耗費資源的,特別是UIImageView嵌套在UIScrollView中的情況下。
如果圖片是從遠端服務(wù)加載的你不能控制圖片大小,比如在下載前調(diào)整到合適大小的話,你可以在下載完成后,最好是用background thread,縮放一次,然后在UIImageView中使用縮放后的圖片。
六.重用大開銷對象
一些objects的初始化很慢,比如NSDateFormatter和NSCalendar。
想要避免使用這個對象的瓶頸你就需要重用他們,可以通過添加屬性到你的class里或者創(chuàng)建靜態(tài)變量來實現(xiàn)。