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 ```
  • 相关阅读:
    结构与算法(04):排序规则与查找算法
    虚拟机系列 | JVM特点,基础结构与执行周期
    Springboot 轻量替代框架 Solon 1.3.10 发布
    Java RPC 框架 Solon 1.3.9 发布,更便利的支持
    Java RPC 框架 Solon 1.3.7 发布,增强Cloud接口能力范围
    分享个本地maven配置
    Java RPC 框架 Solon 1.3.1 发布,推出Cloud接口与配置规范
    使用 quartz-solon-plugin 开发定时任务(新)
    使用 cron4j-solon-plugin 开发定时任务(新)
    国际开源社区OW2成立快应用兴趣小组,助推快应用生态发展
  • 原文地址:https://www.cnblogs.com/mkai/p/5682263.html
Copyright © 2011-2022 走看看