zoukankan      html  css  js  c++  java
  • ios 计算某个文件文件夹的大小

     1  2 ```objc
     3 @implementation NSString (XMGExtension)
     4 //- (unsigned long long)fileSize
     5 //{
     6 //    // 总大小
     7 //    unsigned long long size = 0;
     8 //
     9 //    // 文件管理者
    10 //    NSFileManager *mgr = [NSFileManager defaultManager];
    11 //
    12 //    // 文件属性
    13 //    NSDictionary *attrs = [mgr attributesOfItemAtPath:self error:nil];
    14 //
    15 //    if ([attrs.fileType isEqualToString:NSFileTypeDirectory]) { // 文件夹
    16 //        // 获得文件夹的大小  == 获得文件夹中所有文件的总大小
    17 //        NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
    18 //        for (NSString *subpath in enumerator) {
    19 //            // 全路径
    20 //            NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
    21 //            // 累加文件大小
    22 //            size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
    23 //        }
    24 //    } else { // 文件
    25 //        size = attrs.fileSize;
    26 //    }
    27 //
    28 //    return size;
    29 //}
    30 
    31 - (unsigned long long)fileSize
    32 {
    33     // 总大小
    34     unsigned long long size = 0;
    35 
    36     // 文件管理者
    37     NSFileManager *mgr = [NSFileManager defaultManager];
    38 
    39     // 是否为文件夹
    40     BOOL isDirectory = NO;
    41 
    42     // 路径是否存在
    43     BOOL exists = [mgr fileExistsAtPath:self isDirectory:&isDirectory];
    44     if (!exists) return size;
    45 
    46     if (isDirectory) { // 文件夹
    47         // 获得文件夹的大小  == 获得文件夹中所有文件的总大小
    48         NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
    49         for (NSString *subpath in enumerator) {
    50             // 全路径
    51             NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
    52             // 累加文件大小
    53             size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
    54         }
    55     } else { // 文件
    56         size = [mgr attributesOfItemAtPath:self error:nil].fileSize;
    57     }
    58 
    59     return size;
    60 }
    61 @end
    62 
    63 MKLog(@"%zd", @"/Users/xiaomage/Desktop".fileSize);
    64 ```
    65 
    66 ## 计算文字的宽度
    67 ```objc
    68 CGFloat titleW = [字符串 sizeWithFont:字体大小].width;
    69 CGFloat titleW = [字符串 sizeWithAttributes:@{NSFontAttributeName : 字体大小}].width;
    70 ```
    71 
    72 ## 有透明度的颜色
    73 ```objc
    74 [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.2];
    75 [UIColor colorWithWhite:1.0 alpha:0.2];
    76 [[UIColor whiteColor] colorWithAlphaComponent:0.2];
    77 ```
    78 
    79 ## viewWithTag:内部的大致实现思路
    80 ```objc
    81 @implementation UIView
    82 - (UIView *)viewWithTag:(NSInteger)tag
    83 {
    84     if (self.tag == tag) return self;
    85 
    86     for (UIView *subview in self.subviews) {
    87         return [subview viewWithTag:tag];
    88     }
    89 }
    90 @end
    91 ```
  • 相关阅读:
    Node.js中的Buffer
    移动端页面弹出对话框效果Demo
    简单编写makefile文件,实现GCC4.9编译项目,增加boost库測试等等。。
    Windows下使用静态库
    将 Android* Bullet 物理引擎移植至英特尔® 架构
    Java读书笔记一(异常处理)
    设计模式
    Ignatius and the Princess III(杭电1028)(母函数)
    oracle树操作(select .. start with .. connect by .. prior)
    三期_day02_数据库表设计和开发准备工作
  • 原文地址:https://www.cnblogs.com/mkai/p/5682263.html
Copyright © 2011-2022 走看看