将NSData Length从字节转换为megs
我正在尝试NSLog
的数量我的NSData
对象是目前所有我可以得到的字节通过使用
NSLog(@"%u", myData.length);
那么我将如何改变这个NSLog
语句,所以我可以看到类似的东西
2.00 megs
任何帮助,将不胜感激。
有千字节1024字节和兆字节1024字节,所以…
NSLog(@"File size is : %.2f MB",(float)myData.length/1024.0f/1024.0f);
请注意,这是一个简单的方法,不能真正适应低于1,048,576字节或高于1,073,741,823字节的字节大小。 有关可处理不同文件大小的更完整解决scheme,请参阅: 将大小转换为可读string的ObjC / Cocoa类?
或者对于OS X 10.8+和iOS 6+
NSLog(@"%@",[NSByteCountFormatter stringFromByteCount:myData.length countStyle:NSByteCountFormatterCountStyleFile]);
对于Swift 3,在Mb:
let countBytes = ByteCountFormatter() countBytes.allowedUnits = [.useMB] countBytes.countStyle = .file let fileSize = countBytes.string(fromByteCount: Int64(dataToMeasure!.count)) print("File size: \(fileSize)")