将NSData Length从字节转换为megs

时间:2012-12-10 22:15:23

标签: ios nsdata nslog

我正在尝试NSLog我的NSData对象的megs数量,但是目前我所能得到的只是使用

的字节数
NSLog(@"%u", myData.length);

那么如何更改此NSLog语句以便我可以看到类似

的内容

2.00 megs

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:118)

一千字节有1024字节,兆字节有1024千字节,所以......

NSLog(@"File size is : %.2f MB",(float)myData.length/1024.0f/1024.0f);

请注意,这是一种简单的方法,无法真正适应低于1,048,576字节或高于1,073,741,823字节的字节大小。有关可处理不同文件大小的更完整解决方案,请参阅:ObjC/Cocoa class for converting size to human-readable string?

或者OS X 10.8+和iOS 6 +

NSLog(@"%@", [[NSByteCountFormatter new] stringFromByteCount:data.length]);

在斯威夫特:

print(ByteCountFormatter().string(fromByteCount: Int64(data.count)))

答案 1 :(得分:15)

对于Swift 3,在Mb:

let countBytes = ByteCountFormatter()
countBytes.allowedUnits = [.useMB]
countBytes.countStyle = .file
let fileSize = countBytes.string(fromByteCount: Int64(dataToMeasure!.count))

print("File size: \(fileSize)")

答案 2 :(得分:0)

使用Swift 5.1和iOS 13,您可以使用以下5种方法之一来解决问题。


#1。使用ByteCountFormatter的{​​{3}}类方法

以下示例代码显示了如何实现string(fromByteCount:countStyle:),以便通过自动将字节转换为更合适的存储单元(例如,兆字节)来打印文件大小:

import Foundation

let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let data = try! Data(contentsOf: url)

let byteCount = data.count
print(byteCount) // prints: 2636725

let displaySize = ByteCountFormatter.string(fromByteCount: Int64(byteCount), countStyle: .file)
print(displaySize) // prints: 2.6 MB

#2。使用ByteCountFormatter的{​​{3}}方法

以下示例代码显示了如何实现ByteCountFormatter的{​​{1}},以通过手动将字节转换为兆字节来打印文件大小:

string(fromByteCount:)

#3。使用import Foundation let url = Bundle.main.url(forResource: "image", withExtension: "png")! let data = try! Data(contentsOf: url) let byteCount = data.count print(byteCount) // prints: 2636725 let formatter = ByteCountFormatter() formatter.allowedUnits = [.useMB] formatter.countStyle = .file let displaySize = formatter.string(fromByteCount: Int64(byteCount)) print(displaySize) // prints: 2.6 MB 的{​​{3}}类方法和ByteCountFormatter

以下示例代码显示了如何实现Measurement,以便通过自动将字节转换为更合适的存储单元(例如,兆字节)来打印文件大小:

string(from:countStyle:)

#4。使用import Foundation let url = Bundle.main.url(forResource: "image", withExtension: "png")! let data = try! Data(contentsOf: url) let byteCount = data.count print(byteCount) // prints: 2636725 let byteSize = Measurement(value: Double(byteCount), unit: UnitInformationStorage.bytes) let displaySize = ByteCountFormatter.string(from: byteSize, countStyle: .file) print(displaySize) // prints: 2.6 MB 的{​​{3}}方法和ByteCountFormatter

以下示例代码显示了如何实现Measurement的{​​{1}}和ByteCountFormatter,以通过手动将字节转换为兆字节来打印文件大小:

string(from:)

#5。使用Measurement的{​​{3}}方法和import Foundation let url = Bundle.main.url(forResource: "image", withExtension: "png")! let data = try! Data(contentsOf: url) let byteCount = data.count print(byteCount) // prints: 2636725 let byteSize = Measurement(value: Double(byteCount), unit: UnitInformationStorage.bytes) let formatter = ByteCountFormatter() formatter.allowedUnits = [.useMB] formatter.countStyle = .file let displaySize = formatter.string(from: byteSize) print(displaySize) // prints: 2.6 MB

以下示例代码显示了如何实现MeasurementFormatterMeasurement的{​​{1}},以通过手动将字节转换为兆字节来打印文件大小:

Measurement