计算内存空间

在项目很多地方可能都会查询一些硬件的信息,比如说,计算手机所剩内存空间,我们将信息通过一个视图显示在界面上:

FOUNDATION_EXPORT NSString * const NSFileSystemSize;
FOUNDATION_EXPORT NSString * const NSFileSystemFreeSize;
FOUNDATION_EXPORT NSString * const NSFileSystemNodes;
FOUNDATION_EXPORT NSString * const NSFileSystemFreeNodes;
@interface HTStorageSpaceView()

@property (nonatomic, strong) UIImageView *progressImg;
@property (nonatomic, strong) UIImageView *trackImg;
/**
 *  空间所剩标签展示
 */
@property (nonatomic, strong) UILabel *progressLable;
@end

@implementation HTStorageSpaceView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        _progressImg           = [[UIImageView alloc]init];
        _trackImg              = [[UIImageView alloc]init];
        
        _progressImg.image     = [self createImageWithColor:GREEN_SYSTEM_COLOR];
        _trackImg.image        = [self createImageWithColor:GRAY_LINE_COLOR];
        
        _progressLable         = [[UILabel alloc]init];
        _progressLable.font    = [UIFont systemFontOfSize:12];
        _progressLable.textColor =  WHITECOLOR;
        
        
        _progressLable.textAlignment = NSTextAlignmentCenter;
        
        [self loadSpaceWishFrame:frame];
        
        [self addSubview:_progressImg];
        [self addSubview:_trackImg];
        [self addSubview:_progressLable];
    }
    return self;
}
/**
 *  加载剩余空间
 */
- (void)loadSpaceWishFrame:(CGRect)frame{
    
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    
    //总空间
    float   space     =   [[fattributes objectForKey:NSFileSystemSize] floatValue];
    //所剩空间
    float   freespace =   [[fattributes objectForKey:NSFileSystemFreeSize] floatValue];
    
    
    float free_m  =  freespace / 1024 / 1024 / 1024;
    float space_m =  space / 1024 / 1024 / 1024;
    float proportion = free_m / space_m;
    
    
    _progressImg.frame      = CGRectMake(0, 0,(1 - proportion) * frame.size.width, frame.size.height);
    _trackImg.frame         = CGRectMake((1 - proportion) * frame.size.width , 0, WIDTH - (1 - proportion) * frame.size.width , frame.size.height);
    _progressLable.text     = [NSString stringWithFormat:@"总空间%.1fG/剩余%.1fG",space_m,free_m]; 
    _progressLable.frame    = CGRectMake(0, 0, WIDTH, frame.size.height -2);
    
}
-(UIImage *) createImageWithColor: (UIColor *) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

效果如下:


IMG_4729.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,539评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,294评论 4 61
  • 大脑转不停地把今天白天零零碎碎写的东西整理到一篇文章中去了,共记2239个字。我也没想到自己会有这么想法。[憨笑]...
    不一样的烟火sss阅读 294评论 0 0
  • 墨菲定律:凡事可能出错的,一定就会出错。当你一直在担心事件会发生时,一定会发生的,这就像吸引力法则一样。 管理者不...
    小笨鱼王月阅读 231评论 0 0
  • 每一次的等待都是折磨。 点开回复的那一刻也是战战兢兢。 天哪,又被拒了。 这一次理由倒是言简意赅,即不是我文笔不好...
    _疯子阅读 158评论 0 1