iOS Development article

Source: Internet
Author: User

iOS Development UI article-Lazy Loading

iOS Development UI article-Lazy Loading

1. Lazy Loading Basic

Lazy Loading--also known as deferred loading--is loaded when needed (low efficiency, small memory footprint). The so-called lazy load, write is its get method.

Note: If it is lazy loading, it is important to be aware of whether there is already, if not then to instantiate

2. Benefits of using lazy loading:

(1) You do not have to write all the code of the created object in the Viewdidload method, the code is more readable

(2) Each control's getter method is responsible for the respective instantiation processing, the code is independent of each other, loosely coupled

//
Yyviewcontroller.m
03-Image Browser Preliminary
//
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]];
First Get and 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);
}

Lazy Loading
/**1. The image's ordinal label */
-(UILabel *) Firstlab
{
Determine if there is already, and if not, instantiate
if (!_firstlab) {
_firstlab=[[uilabel Alloc]initwithframe:cgrectmake (20, 10, 300, 30)];
[_firstlab Settextalignment:nstextalignmentcenter];
[Self.view Addsubview:_firstlab];
}
return _firstlab;
}

/**2. Lazy Loading of picture controls */
-(Uiimageview *) icon
{
Determine if there is already, and if not, instantiate
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. Describing lazy loading of controls */
-(UILabel *) Lastlab
{
Determine if there is already, and if not, instantiate
if (!_lastlab) {
_lastlab=[[uilabel Alloc]initwithframe:cgrectmake (20, 400, 300, 30)];
[_lastlab Settextalignment:nstextalignmentcenter];
[Self.view Addsubview:_lastlab];
}
return _lastlab;
}

/**4. Delay loading of the left-click button */
-(UIButton *) leftbtn
{
Determine if there is already, and if not, instantiate
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. Lazy loading of right-click buttons */
-(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;
}

Get method for array
-(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

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------

Lazy loading, also known as deferred loading. The popular point is that in development, when the application needs to use the resources. Do not load resources when the program is started, and then load the resources only when they are running when some resources are needed.

We know that iOS devices have limited memory, and if you load all the resources that will be used in the future once the program is started, you may run out of memory on your iOS device. These resources such as large amounts of data, pictures, audio, etc.

Here's an example:

1> Define control Properties, note: The property must be strong, the sample code is as follows:

@property (nonatomic, strong) Nsarray *imagelist;

2> lazy Loading in the getter method of the property, the sample code is as follows:

Lazy loading-when needed, in instantiation loaded into memory-(Nsarray *) imagelist{    //Only when the Getter method is called for the first time, is empty, then instantiates and establishes the array    if (_imagelist = = nil) {        //file means loading files        from the full path of the file NSString *path = [[NSBundle mainbundle] pathforresource:@ "ImageData" oftype:@ "plist"];        NSLog (@ "%@", path);                _imagelist = [Nsarray Arraywithcontentsoffile:path];    }        return _imagelist;}

As the above code, there is a _imagelist property, if in the code of the program, there are multiple accesses to the _imagelist property, such as the following

self.imagelist; self.imagelist; self.imagelist;

Although the _imagelist property was accessed 3 times, the imageList array is not empty when the imageList zodiac is accessed for the first time.
ImageList! = nil When accessing imageList for the second time; The program will not execute the following code

NSString *path = [[NSBundle mainbundle] pathforresource:@ "ImageData" oftype:@ "plist"];        NSLog (@ "%@", path);                _imagelist = [Nsarray Arraywithcontentsoffile:path];

The data will not be loaded in the Plist file again.

Benefits of Lazy Loading:

1> do not have to write the code of the object in the Viewdidload method, the code is more readable

2> Each property's getter method is responsible for the respective instantiation, the code is independent of each other, loosely coupled

3> only when resources are really needed, and then load, saving memory resources.

Reminder: This is what Apple is advocating. In fact, Apple's iOS system has been used in many parts of the way lazy loading, such as the creation of the controller's view.

iOS Development article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.