FPS calculation method
FPS is the abbreviation for frame per second, which is the number of frames per second. This term is widely used in computer graphics, video capture, games and so on.
Cadisplaylink
Cadisplaylink is a timer that allows us to draw content to the screen at the same frequency as the screen refresh rate, create a new Cadisplaylink object, add it to a runloop, and provide it with a target and selector Called when the screen is refreshed. Once the Cadisplaylink has a specific mode registered to the Runloop lag, the runloop invokes the selector on the bound target whenever the screen needs to be refreshed. At this point, target can read the timestamp of each call to Cadisplaylink.
1 _link = [Cadisplaylink displaylinkwithtarget:[yyweakproxy proxywithtarget:self] Selector: @selector (tick:)]; 2 [_link Addtorunloop:[nsrunloop Mainrunloop] formode:nsrunloopcommonmodes];
1- (void) Tick: (Cadisplaylink *) Link {2 if(_lasttime = =0) {3_lasttime =Link.timestamp;4 return;5 }6 7_count++;8Nstimeinterval Delta = link.timestamp-_lasttime;9 if(Delta <1)return;Ten_lasttime =Link.timestamp; One floatfps = _count/Delta; A_count =0; - -CGFloat progress = fps/60.0; theUicolor *color = [Uicolor Colorwithhue:0.27* (Progress-0.2) Saturation:1Brightness:0.9Alpha1]; - -nsmutableattributedstring *text = [[Nsmutableattributedstring alloc] initwithstring:[nsstring StringWithFormat:@"%d FPS",(int) Round (fps)]; -[Text Setcolor:color Range:nsmakerange (0, Text.length-3)]; +[Text Setcolor:[uicolor Whitecolor] Range:nsmakerange (Text.length-3,3)]; -Text.font =_font; +[Text Setfont:_subfont range:nsmakerange (Text.length-4,1)]; A atSelf.attributedtext =text; -}
Code reference: Yyfpslabel.
Problem:
1. [Yyweakproxy Proxywithtarget:self] How to avoid circular references.
eg
[NSTimer timerWithTimeInterval:1.f target:self selector:@selector(tick:) userInfo:nil repeats:YES];
CADisplayLink *_link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
The above two usages, will be the self strong reference, at this time the timer holds self,self also holds the timer, the circular reference causes the page dismiss, both sides cannot release, causes the circular reference.
The use of __weak at this time cannot be solved effectively:
__weak typeof(self) weakSelf = self;_link = [CADisplayLink displayLinkWithTarget:weakSelf selector:@selector(tick:)];
FPS of Yykit Notes