[IOS 開發] 懶加載 (延遲加載) 的基本方式,好處,代碼示例


 

懶加載的好處:

1> 不必將創建對象的代碼全部寫在viewDidLoad方法中,代碼的可讀性更強

2> 每個屬性的getter方法中分別負責各自的實例化處理,代碼彼此之間的獨立性強,松耦合

3>只有當真正需要資源時,再去加載,節省了內存資源。

 

1.懶加載基本

 

我們知道iOS設備的內存有限,如果在程序在啟動后就一次性加載將來會用到的所有資源,那么就有可能會耗盡iOS設備的內存。這些資源例如大量數據,圖片,音頻等等

懶加載——也稱為延遲加載,說的通俗一點,就是在開發中,當程序中需要利用的資源時。在程序啟動的時候不加載資源,只有在運行當需要一些資源時,再去加載這些資源,即在需要的時候才加載(效率低,占用內存小),所謂懶加載,寫的是其get方法.

 

提醒:這是蘋果公司提倡的做法。其實蘋果公司做的IOS系統中很多地方都用到了懶加載的方式,比如控制器的View的創建。

注意:如果是懶加載的話則一定要注意先判斷是否已經有了,如果沒有那么再去進行實例化

 

2.使用懶加載的好處:

(1)不必將創建對象的代碼全部寫在viewDidLoad方法中,代碼的可讀性更強

(2)每個控件的getter方法中分別負責各自的實例化處理,代碼彼此之間的獨立性強,松耦合

 

3.代碼示例

 

代碼示例1:

 

1> 定義控件屬性,注意:屬性必須是strong的,示例代碼如下:

@property (nonatomic, strong) NSArray *imageList;

 

2> 在屬性的getter方法中實現懶加載,示例代碼如下:

// 懶加載-在需要的時候,在實例化加載到內存中
- (NSArray *)imageList
{
    // 只有第一次調用getter方法時,為空,此時實例化並建立數組
    if (_imageList == nil) {
        // File表示從文件的完整路徑加載文件
        NSString *path = [[NSBundle mainBundle] pathForResource:@"ImageData" ofType:@"plist"];
        NSLog(@"%@", path);
        
        _imageList = [NSArray arrayWithContentsOfFile:path];
    }
    
    return _imageList;
}

 

如上面的代碼,有一個_imageList屬性,如果在程序的代碼中,有多次訪問_imageList屬性,例如下面

self.imageList ;

self.imageList ;

self.imageList ;

 

雖然訪問了3次_imageList 屬性,但是當第一次訪問了imageList屬相,imageList數組就不為空,
當第二次訪問imageList 時  imageList != nil;程序就不會執行下面的代碼

NSString *path = [[NSBundle mainBundle] pathForResource:@"ImageData" ofType:@"plist"];
        NSLog(@"%@", path);
        
        _imageList = [NSArray arrayWithContentsOfFile:path];

 

就不會再次在PList文件中加載數據了。

 

 

===========================

 

 

代碼示例2:

//
//  YYViewController.m
//  03-圖片瀏覽器初步
//
//  Created by apple on 14-5-21.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

#define POTOIMGW    200
#define POTOIMGH    300
#define POTOIMGX    60
#define  POTOIMGY    50

@interface YYViewController ()

@property(nonatomic,strong)UILabel *firstlab;
@property(nonatomic,strong)UILabel *lastlab;
@property(nonatomic,strong)UIImageView *icon;
@property(nonatomic,strong)UIButton *leftbtn;
@property(nonatomic,strong)UIButton *rightbtn;
@property(nonatomic,strong)NSArray *array;
@property(nonatomic ,assign)int i;
-(void)change;
@end



@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self change];
}

-(void)change
{
    [self.firstlab setText:[NSString stringWithFormat:@"%d/5",self.i+1]];
    //先get再set
    
    self.icon.image=[UIImage imageNamed:self.array[self.i][@"name"]];
    self.lastlab.text=self.array[self.i][@"desc"];
  
    self.leftbtn.enabled=(self.i!=0);
    self.rightbtn.enabled=(self.i!=4);
}

//延遲加載
/**1.圖片的序號標簽*/
-(UILabel *)firstlab
{
    //判斷是否已經有了,若沒有,則進行實例化
    if (!_firstlab) {
        _firstlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 30)];
        [_firstlab setTextAlignment:NSTextAlignmentCenter];
        [self.view addSubview:_firstlab];
    }
    return _firstlab;
}

/**2.圖片控件的延遲加載*/
-(UIImageView *)icon
{
     //判斷是否已經有了,若沒有,則進行實例化
    if (!_icon) {
        _icon=[[UIImageView alloc]initWithFrame:CGRectMake(POTOIMGX, POTOIMGY, POTOIMGW, POTOIMGH)];
        UIImage *image=[UIImage imageNamed:@"biaoqingdi"];
        _icon.image=image;
        [self.view addSubview:_icon];
    }
    return _icon;
}

/**3.描述控件的延遲加載*/
-(UILabel *)lastlab
{
     //判斷是否已經有了,若沒有,則進行實例化
    if (!_lastlab) {
        _lastlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 400, 300, 30)];
        [_lastlab setTextAlignment:NSTextAlignmentCenter];
        [self.view addSubview:_lastlab];
    }
    return _lastlab;
}

/**4.左鍵按鈕的延遲加載*/
-(UIButton *)leftbtn
{
     //判斷是否已經有了,若沒有,則進行實例化
    if (!_leftbtn) {
        _leftbtn=[UIButton buttonWithType:UIButtonTypeCustom];
        _leftbtn.frame=CGRectMake(0, self.view.center.y, 40, 40);
        [_leftbtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
        [_leftbtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
        [self.view addSubview:_leftbtn];
        [_leftbtn addTarget:self action:@selector(leftclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftbtn;

}

/**5.右鍵按鈕的延遲加載*/
-(UIButton *)rightbtn
{
    if (!_rightbtn) {
        _rightbtn=[UIButton buttonWithType:UIButtonTypeCustom];
        _rightbtn.frame=CGRectMake(POTOIMGX+POTOIMGW+10, self.view.center.y, 40, 40);
        [_rightbtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
        [_rightbtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
        [self.view addSubview:_rightbtn];
        [_rightbtn addTarget:self action:@selector(rightclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightbtn;
}

//array的get方法
-(NSArray *)array
{
    if (_array==nil) {
        NSString *path=[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
        _array=[[NSArray alloc]initWithContentsOfFile:path];
    }
    return _array;
}

-(void)rightclick:(UIButton *)btn
{
    self.i++;
    [self change];
}

-(void)leftclick:(UIButton *)btn
{
    self.i--;
    [self change];
}

@end

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM