iOS 懒加载

来源:互联网 发布:首席数据官 招聘 编辑:程序博客网 时间:2024/05/16 18:42

1.懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小)。所谓懒加载,写的是其getter方法。说的通俗一点,就是在开发中,当程序中需要利用的资源时。在程序启动的时候不加载资源,只有在运行当需要一些资源时,再去加载这些资源。

我们知道iOS设备的内存有限,如果在程序在启动后就一次性加载将来会用到的所有资源,那么就有可能会耗尽iOS设备的内存。这些资源例如大量数据,图片,音频等等,所以我们在使用懒加载的时候一定要注意先判断是否已经有了,如果没有那么再去进行实例化

2.使用懒加载的好处:

(1)不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强

(2)每个控件的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合

(3)只有当真正需要资源时,再去加载,节省了内存资源。

<span style="font-size:14px;">//#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong) UILabel *noLabel;@property (nonatomic, strong) UIImageView *iconImage;@property (nonatomic, strong) UILabel *descLabel;@property (nonatomic, strong) UIButton *leftButton;@property (nonatomic, strong) UIButton *rightButton;@property (nonatomic, strong) UILabel *name;/**当前显示的照片索引*/@property (nonatomic, assign) NSInteger index;@property (nonatomic, strong) NSArray *imageList;@end@implementation ViewController/** 懒加载(延迟加载),通过getter方法实现 效果:让对象在最需要的时候才创建! */- (NSArray *)imageList{    if (_imageList == nil) {        //"包" Bundle  NSBundle mainBundle编译安装之后对应的程序包        NSString *path = [[NSBundle mainBundle] pathForResource:@"ImageList" ofType:@"plist"];        //在oc中看到了ContentsOfFile,通常需要完整的路径        _imageList = [NSArray arrayWithContentsOfFile:path];    }    return _imageList;}#pragma mark - 控件的懒加载//在getter方法中,不要再使用self-(UILabel *)noLabel{    if (_noLabel == nil) {        _noLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 40)];        _noLabel.textAlignment = NSTextAlignmentCenter;//调整居中        [self.view addSubview:_noLabel];    }    return _noLabel;}- (UIImageView *)iconImage{    if (_iconImage == nil) {        CGFloat imageW = 250;        CGFloat imageH = 400;        CGFloat imageX = (self.view.bounds.size.width - imageW)*0.5;        CGFloat imageY = CGRectGetMaxY(self.noLabel.frame) + 20;        _iconImage = [[UIImageView alloc] initWithFrame: CGRectMake(imageX, imageY, imageW, imageH)];        [self.view addSubview:_iconImage];    }    return _iconImage;}- (UILabel *)descLabel{    if (_descLabel == nil) {        CGFloat descY = CGRectGetMaxY(self.iconImage.frame);        _descLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, descY, self.view.bounds.size.width, 100)];        //_descLabel.text = @"高圆圆";        _descLabel.textAlignment = NSTextAlignmentCenter;        //需要label具有足够的高度,不限制显示的行数        _descLabel.numberOfLines = 0;        [self.view addSubview:_descLabel];    }    return _descLabel;}-(UIButton *)leftButton{    if (_leftButton == nil) {        _leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];        CGFloat centerY = self.iconImage.center.y;        CGFloat centerX = self.iconImage.frame.origin.x*0.5;        _leftButton.center = CGPointMake(centerX, centerY);        [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];        [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];        _leftButton.tag = -1;        [self.view addSubview:_leftButton];        [_leftButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];    }    return _leftButton;}-(UIButton *)rightButton{    if (_rightButton == nil) {        CGFloat centerY = self.iconImage.center.y;        CGFloat centerX = self.iconImage.frame.origin.x*0.5;        _rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];        _rightButton.center = CGPointMake(self.view.bounds.size.width -centerX, centerY);        [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];        [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];        _rightButton.tag = 1;        [self.view addSubview:_rightButton];                [_rightButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];    }    return _rightButton;}- (UILabel *)name{    if (_name == nil) {        _name = [[UILabel alloc] initWithFrame:CGRectMake(0, 600, self.view.bounds.size.width, 40)];        _name.text = @"现代范儿";        _name.textAlignment = NSTextAlignmentCenter;//调整居中        [self.view addSubview:_name];    }    return _name;}- (void)viewDidLoad {    // Do any additional setup after loading the view, typically from a nib.    [super viewDidLoad];        //显示照片信息    [self showPhoto];}-(void) showPhoto{    //设置序号    self.noLabel.text = [NSString stringWithFormat:@"%lu/%d", self.index + 1, 5];       //设置图像和描述    self.iconImage.image = [UIImage imageNamed:self.imageList[self.index][@"name"]];    self.descLabel.text = self.imageList[self.index][@"desc"];    self.rightButton.enabled = (self.index != 4);    self.leftButton.enabled = (self.index != 0);}/**在oc中,很多方法的第一个参数,都是触发该方法的对象*/-(void) clickButton: (UIButton *) button{    //根据按钮调整当前显示图片的索引    self.index += button.tag;    [self showPhoto];}@end


0 0
原创粉丝点击