计算内存空间

//
//  ViewController.m
//  计算内存空间
//
//  Created by yeyuanxiang on 16/5/19.
//  Copyright © 2016年 KuYu_YX. All rights reserved.
//

#import "ViewController.h"

#define WIDTH self.view.bouns.size.width

#define HEIGHT self.view.bouns.size.height

@interface ViewController ()

@property (strong,nonatomic) UIImageView *progressImg ;

@property (strong,nonatomic) UIImageView *trackImg ;

//空间所剩的标签展示

@property (strong,nonatomic) UILabel *progressLable ;

@end

@implementation ViewController



/**
 *  @author KuYu_YX
 *
 在项目很多地方可能都会查询一些硬件的信息,比如说,计算手机所剩内存空间,我们将信息通过一个视图显示在界面上:
 FOUNDATION_EXPORT NSString * const NSFileSystemSize;
 FOUNDATION_EXPORT NSString * const NSFileSystemFreeSize;
 FOUNDATION_EXPORT NSString * const NSFileSystemNodes;
 FOUNDATION_EXPORT NSString * const NSFileSystemFreeNodes;
 *
 *  @since <#1.0#>
 */


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    
    
    
    
}





- (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.view addSubview:_progressImg];
        [self.view addSubview:_trackImg];
        [self.view 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;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值