How to get the file size given a path?

后端 未结 10 1185
自闭症患者
自闭症患者 2020-11-30 20:22

I have a path to file contained in an NSString. Is there a method to get its file size?

10条回答
  •  伪装坚强ぢ
    2020-11-30 20:41

    If you want only file size with bytes just use,

    unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:yourAssetPath error:nil] fileSize];
    

    NSByteCountFormatter string conversion of filesize (from Bytes) with precise KB, MB, GB ... Its returns like 120 MB or 120 KB

    NSError *error = nil;
    NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:yourAssetPath error:&error];
    if (attrs) {
        NSString *string = [NSByteCountFormatter stringFromByteCount:fileSize countStyle:NSByteCountFormatterCountStyleBinary];
        NSLog(@"%@", string);
    }
    

提交回复
热议问题