Memory Optimization: encapsulation of jiugong Ge; Memory Optimization encapsulation of jiugong
As more and more apps are available on the market, many software require a lot of memory for mobile phones. Therefore, we must master how to optimize the memory and optimize our apps as much as possible during development. Today, let's take a look at the optimization of jiugongge. Below is the software
1. In order to achieve better results, we do not need UITableView. First, we need to use XIB to customize an image and text.
2. Customize the main view JRMainScrollView and implement functions through Protocol proxy. The procedure is similar to that of UITableView. For details, refer to UITableView.
First, we need to define the data source protocol.
// Data source protocol @ protocol JRMainScrollDataSource <NSObject> // obtain the total number-(NSInteger) numberOfItems; // obtain the number of columns-(NSInteger) numberOfColumsOfRow; // obtain item-(UIView *) mainScrollView :( JRMainScrollView *) mainScrollView itemAtIndex :( NSInteger) index; @ end
Second, we need to define the property protocol.
// Attribute protocol @ protocol publish <NSObject> @ optional // obtain the height-(CGFloat) Publish :( JRMainScrollView *) mainScrollView; // obtain the width-(CGFloat) widthForItemAtView :( JRMainScrollView *) mainScrollView; // obtain the spacing-(CGFloat) mainScrollView :( JRMainScrollView *) mainScrollView spaceForItemWithType :( kJRMainScrollItemSpace) type; @ end
Note that you can define an enumeration to obtain the spacing between left and right and the vertical and vertical distances.
typedef enum{ kJRMainScrollItemLeftSpace, kJRMainScrollItemTopSpace} kJRMainScrollItemSpace;
3. Implement the internal layout, calculate all the current frames, and put them into the array. During this period, the attribute parameters used must be obtained from the proxy. The Code is as follows:
// Load the subview-(void) _ loadSubViews {// obtain the total number and number of columns NSInteger totalItems = [self. jrDataSource numberOfItems]; NSInteger colum = [self. jrDataSource numberOfColumsOfRow]; // obtain the width and height of CGFloat itemWidth = [self. jrDelegate widthForItemAtView: self]; CGFloat itemheimpaired = [self. jrDelegate heightForItemAtView: self]; // obtain the upper and lower spacing CGFloat leftSpace = [self. jrDelegate mainScrollView: self spaceForItemWithType: kJRMainScrollItemLeftSpace]; CGFloat topSpace = [self. jrDelegate mainScrollView: self spaceForItemWithType: kJRMainScrollItemTopSpace]; CGFloat space = (kWidth-2 * leftSpace-colum * itemWidth)/(colum-1) + itemWidth; for (int I = 0; I <totalItems; I ++) {int clo = I % colum; int row = I/colum; CGRect frame = CGRectMake (leftSpace + clo * space, 20 + row * (itemheimpaired + topSpace ), itemWidth, itemheimpaired); [self. array addObject: [NSValue valueWithCGRect: frame];} self. contentSize = CGSizeMake (0, CGRectGetMaxY ([self. array lastObject] CGRectValue]); self. showsVerticalScrollIndicator = NO ;}
4. Check whether the current frame is within the visible range of the current screen. If the current frame is being rendered, ignore it.
-(Void) layoutSubviews {[super layoutSubviews]; // the frame for (int I = 0; I <self. array. count; I ++) {UIView * tempView = (UIView *) self. current [@ (I)]; CGRect rect = [self. array [I] CGRectValue]; if ([self isInScreenWith: rect]) {if (! TempView) {// you need to re-load the UIView * view = [self. jrDataSource mainScrollView: self itemAtIndex: I]; view. frame = rect; [self. current setObject: view forKey: @ (I)]; [self addSubview: view] ;}} else if (tempView) {// remove [self. current removeObjectForKey: @ (I)]; [tempView removeFromSuperview]; [self. pool addObject: tempView] ;}// check whether the field is inside the field of view. In two cases, the Y value is inside the screen or the MAXY value is inside the screen-(BOOL) isInScreenWith :( CGRect) frame {CGFloat setMiny = self. contentOffset. y; CGFloat setMaxy = self. contentOffset. y + kHeight; BOOL condition1 = frame. origin. y> = setMiny & frame. origin. y <= setMaxy; BOOL condition2 = CGRectGetMaxY (frame)> = setMiny & CGRectGetMaxY (frame) <= setMaxy; if (condition1 | condition2) {return YES ;} return NO ;}
5. Reuse objects in the Operation Buffer Pool
/** Save frame */@ property (nonatomic, strong) NSMutableArray * array;/** store the currently displayed object */@ property (nonatomic, strong) NSMutableDictionary * current; /** store the buffer pool object */@ property (nonatomic, strong) NSMutableSet * pool; /*** get the reuse object ** @ param identy <# identy description #> ** @ return <# return value description #> */-(JRRectView *) dequeueReusedItemWithIdenty :( NSString *) identy {JRRectView * view = [self. pool anyObjec T]; if (view! = Nil) {[self. pool removeObject: view];} return view ;}
6. Load the view on the master controller and implement the proxy method.
// Load all data-(void) _ loadSubviews {// 1 Add the rolling view JRMainScrollView * mainScroll = [[JRMainScrollView alloc] initWithFrame: self. view. bounds]; mainScroll. jrDataSource = self; mainScroll. jrDelegate = self; [mainScroll reloadViews]; [self. view addSubview: mainScroll] ;}# pragma mark-data source method-(NSInteger) numberOfItems {return 132 ;}- (NSInteger) numberOfColumsOfRow {return 3 ;}- (UIView *) mainScrollView :( JRMainScrollView *) mainScrollView itemAtIndex :( NSInteger) index {JRRectView * cell = [mainScrollView dequeueReusedItemWithIdenty: @ "test"]; if (cell = nil) {cell = [[[NSBundle mainBundle] loadNibNamed: @ "rect" owner: nil options: nil] lastObject];} cell. titleLabel. text = [NSString stringWithFormat: @ "Download"]; NSString * imageName = [NSString stringWithFormat: @ "% d", arc4random_uniform (20) + 256]; UIImage * image = [UIImage imageNamed: imageName]; cell. image. image = image; return cell;} # pragma mark-proxy method // obtain height-(CGFloat) heightForItemAtView :( JRMainScrollView *) mainScrollView {return 100 ;} // obtain the width-(CGFloat) widthForItemAtView :( JRMainScrollView *) mainScrollView {return 90;} // obtain the spacing-(CGFloat) mainScrollView :( JRMainScrollView *) mainScrollView resume :( partial) type {if (type = kJRMainScrollItemLeftSpace) {return 20;} else if (type = kJRMainScrollItemTopSpace) {return 20;} return 20 ;}
For more information, clickView Source CodeRun the test in person.
For questions or technical exchanges, please join the official QQ group: (452379712)
Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection on the article page, otherwise, you are entitled to pursue legal liability.