查看iOS屏幕帧数MGFPSStatus

作者: geekAppke | 来源:发表于2016-12-26 02:33 被阅读162次

    在状态栏显示FPS状态,FPS是一秒钟渲染多少帧 Frame Per Second = FPS,FPS值为55~60最佳,低于这个范围就较为卡顿了。

    使用方法

    #if defined(DEBUG) || defined(_DEBUG)
      // 添加FPSStatus
      [[MGFPSStatus shareInstance] show];
    #endif
    
    状态栏MGFPSStatus

    FPSStatus.m

    @implementation MGFPSStatus {
        // 顶部FPSLabel
        UILabel *_fpsLabel;
        
        CADisplayLink *_displayLink;
        NSTimeInterval _lastTime;
        NSUInteger _count;
        
        UIColor *_fpsColor;
    }
    
    + (instancetype)shareInstance
    {
        static MGFPSStatus *_instance;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            
            _instance = [[MGFPSStatus alloc] init];
        });
        return _instance;
    }
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            
            // 创建label
            _fpsLabel = [[UILabel alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width)/2+30, 0, 50, 20)];
            
    //        _fpsLabel.text = @"23344";
            _fpsLabel.font = [UIFont systemFontOfSize:12];
            _fpsLabel.layer.cornerRadius = 5;
            _fpsLabel.clipsToBounds = YES;
            _fpsLabel.textAlignment = NSTextAlignmentCenter;
            _fpsLabel.userInteractionEnabled = NO;
            _fpsLabel.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.700];
            
            // 创建定时器
            _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
            [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
        }
        return self;
    }
    
    - (void)tick:(CADisplayLink *)link {
        // 第一次
        if (_lastTime == 0) {
            _lastTime = link.timestamp;
            return;
        }
        
        _count++;
        NSTimeInterval delta = link.timestamp - _lastTime;
        
        if (delta < 1) return;
        _lastTime = link.timestamp;
        float fps = _count / delta;
        _count = 0;
        
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d FPS",(int)round(fps)]];
        
        // 根据卡顿程度显示颜色
        if (fps >= 55.0) {
            _fpsColor = [UIColor greenColor];
        } else if (fps>=50 && fps<55) {
            _fpsColor = [UIColor yellowColor];
        } else {
            _fpsColor = [UIColor redColor];
        }
        
        [text addAttribute:NSForegroundColorAttributeName value:_fpsColor range:NSMakeRange(0, text.length - 3)];
        [text addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(text.length - 3, 3)];
        _fpsLabel.attributedText = text;
    }
    - (void)dealloc
    {
        [_displayLink invalidate];
        NSLog(@"release");
    }
    
    - (void)show {
        [[UIApplication sharedApplication].keyWindow addSubview:_fpsLabel];
    }
    @end
    

    参考资料

    iOS查看屏幕帧数工具--YYFPSLabel
    iOS 性能调优,成为一名合格iOS程序员必须掌握的技能
    JPFPSStatus
    YYKit笔记之FPS

    相关文章

      网友评论

        本文标题:查看iOS屏幕帧数MGFPSStatus

        本文链接:https://www.haomeiwen.com/subject/ayodvttx.html