计算内存空间

作者: 南方_H | 来源:发表于2016-03-13 22:21 被阅读397次

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

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

相关文章

  • 计算内存空间

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

  • 入门数据结构(1)

    数据结构的理解 数据结构是用来整理计算机的内存空间的,也就是合理的利用计算机的内存空间.他也是表示计算机在现实的存...

  • 理解 weak、assign 之前,你还需要知道这些

    程序的本质就是操作内存空间。执行一段代码,本质就是告诉计算机怎么找到,并操作内存空间。每片内存空间,在操作之前都需...

  • 开辟内存空间

    实例对象的内藏所占大小计算完成后,接下来要做的就是开辟内存空间了。开辟内存空间的源码在 libmalloc。 我们...

  • JVM内存空间大小计算

  • OC 内存对其原理分析

    上一篇文章我们讲到 alloc 在开辟内存空间之前,对对要分配的内存空间提前进行计算,并最终使用 16 字节对其方...

  • 变量与字符串

    1 变量 (1) 简单来说是可以被赋值的量(2) 定义变量时,程序会在计算机内存空间划分一个内存空间给该变量存储数...

  • 1.3原码/反码/补码

    数据存储:计算机储存数据,先开辟内存空间,再存储数据,计算机开辟内存的最小单位是字节,二进制; 原码/反码/补码是...

  • hadoop namenode 元数据计算公式

    元数据占用内存空间说明 对象类别 估算大小(bytes) 计算方法 估计总大小(bytes) 文件 224 224...

  • 三、结构体(Struct)/联合体(Union)/位域

    对象的创建核心有三步: cls->instanceSize:计算需要开辟的内存空间大小 calloc:申请内存,返...

网友评论

本文标题:计算内存空间

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