With more and more apps on the market online, a lot of software on the phone memory requirements is also very large, so we must be in the development of how to optimize the memory, to optimize their own apps as much as possible. Today we'll take a look at the optimization of the nine Gongge. The following is the software
1, in order to achieve better results we do not need to UITableView, first we want to customize a picture and text through Xib
2, the custom main view Jrmainscrollview, through the protocol proxy to realize the function, the practice and UITableView similar, everybody may refer to UITableView
First: We want to define the data source protocol
<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >// Data Source protocol @protocol Jrmainscrolldatasource <NSObject>// get the total quantity - ( Nsinteger) Numberofitems; // gets the number of columns - (Nsinteger) Numberofcolumsofrow; // Get Item-(UIView *) Mainscrollview: (Jrmainscrollview *) Mainscrollview Itematindex: (Nsinteger) index;@ End</span>
Second: We want to define the attribute agreement
<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >// Property Agreement @protocol jrmainscrolldelegate <NSObject>@optional// Get height -(CGFloat) Heightforitematview: (Jrmainscrollview *) Mainscrollview; // Get width -(cgfloat) Widthforitematview: (Jrmainscrollview *) Mainscrollview; // Get Pitch -(CGFloat) Mainscrollview: (Jrmainscrollview *) Mainscrollview Spaceforitemwithtype: ( kjrmainscrollitemspace) type; @end</span>
Note The Get spacing includes the spacing to the left and right and the top and bottom spacing by defining an enumeration implementation
enum { kjrmainscrollitemleftspace, kjrmainscrollitemtopspace} kjrmainscrollitemspace; </span>
3, the internal layout of the implementation, calculate all the current frame, and into the array during this time, the use of the attribute parameters need to be obtained from the agent, the code is as follows
<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >//To load a child view- (void) _loadsubviews{//get total number and number of columnsNsinteger totalitems=[Self.jrdatasource Numberofitems]; Nsinteger Colum=[Self.jrdatasource Numberofcolumsofrow]; //get width and heightCGFloat itemwidth=[Self.jrdelegate widthforitematview:self]; CGFloat itemheigt=[Self.jrdelegate heightforitematview:self]; //get up and down spacingCGFloat 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(inti=0;i<totalitems;i++) { intclo=i%Colum; introw=i/Colum; CGRect Frame=cgrectmake (Leftspace+clo*space, 20+row* (itemheigt+topspace), Itemwidth, itemheigt); [Self.array Addobject:[nsvalue Valuewithcgrect:frame]; }self.contentsize=cgsizemake (0, Cgrectgetmaxy ([[Self.array Lastobject] cgrectvalue]); Self.showsverticalscrollindicator=NO;}</span>
4, judge whether the current frame is within the current screen visual range, if you are in the rendering of the view, if not ignored.
<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >-(void) layoutsubviews{[SuperLayoutsubviews]; //Loop facilitates getting a frame in the screen for(inti=0;i<self.array.count;i++) {UIView* Tempview= (UIView *) self.current[@ (i)]; CGRect rect=[Self.array[i] cgrectvalue]; if([self isinscreenwith:rect]) {if(!tempview) {//There's nothing in the dictionary that needs a lot of new loading.UIView *view=[Self.jrdatasource mainscrollview:self itematindex:i]; View.frame=rect; [Self.current Setobject:view forkey:@ (i)]; [Self addsubview:view]; } }Else if(Tempview) {//If a dictionary is present and is not inside the line of sight, remove[Self.current removeobjectforkey:@ (i)]; [Tempview Removefromsuperview]; [Self.pool Addobject:tempview]; } }//Judging is not within the field of view, where there are 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) { returnYES; } returnNO; }</span>
5. Operation Buffer Pool Reuse objects
<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >/**Store Frame*/@property (nonatomic,strong) Nsmutablearray*Array;/**store the currently displayed object*/@property (nonatomic,strong) nsmutabledictionary*Current ;/**Storing buffer pool objects*/@property (nonatomic,strong) Nsmutableset*Pool;/*** GET REUSABLE objects * *@paramIdenty < #identy description#> * *@return< #return value description#>*/-(Jrrectview *) Dequeuereuseditemwithidenty: (NSString *) identy{Jrrectview* view=[Self.pool Anyobject]; if(view!=Nil) {[Self.pool Removeobject:view]; } returnview;}</span>
6. Load the view in the host controller and implement the Proxy method
<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >//Load All data- (void) _loadsubviews{//1 Adding scrolling viewsJrmainscrollview * mainscroll=[[Jrmainscrollview alloc] initWithFrame:self.view.bounds]; Mainscroll.jrdatasource=Self ; Mainscroll.jrdelegate=Self ; [Mainscroll Reloadviews]; [Self.view Addsubview:mainscroll];} #pragma Mark-Data Source Methods-(Nsinteger) numberofitems{return132;}-(Nsinteger) numberofcolumsofrow{return3;}-(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; returnCell;} #pragma Mark-Proxy Method//Get Height-(CGFloat) Heightforitematview: (Jrmainscrollview *) mainscrollview{return100;}//Get width-(CGFloat) Widthforitematview: (Jrmainscrollview *) mainscrollview{return90;}//Get spacing-(CGFloat) Mainscrollview: (Jrmainscrollview *) Mainscrollview Spaceforitemwithtype: (kjrmainscrollitemspace) type{if(type==kjrmainscrollitemleftspace) { return20; }Else if(type==kjrmainscrollitemtopspace) { return20; } return20;}</span>
7, memory optimization, in addition to the above 6 points, but also to understand the optimization of the source code , a key to clear log (development log) information, one-click Optimization to reduce the size of the original packet after encryption
Android Memory optimized package nine lattice