将NSData长度从字节转换为兆字节

41

我正在尝试使用NSLog打印我的NSData对象的大小,但是目前我只能获取字节大小,方法如下:

NSLog(@"%u", myData.length);

那么我该如何修改这个NSLog语句,以便我可以看到类似于2.00 megs的内容?任何帮助将不胜感激。

我该如何格式化它,以便在后面显示两位小数。 - HurkNburkS
3个回答

128
一个千字节等于1000字节,一个兆字节等于1000千字节,所以...
NSLog(@"File size is : %.2f MB",(float)myData.length/1000.0f/1000.0f);

请注意,这只是一种简单的方法,不能处理特别大或特别小的数字。更通用的解决方案是使用苹果的NSByteCountFormatter:
或者适用于OS X 10.8+和iOS 6+。
NSLog(@"%@", [[NSByteCountFormatter new] stringFromByteCount:data.length]);

在Swift中:
print(ByteCountFormatter().string(fromByteCount: Int64(data.count)))

1
太棒了,非常感谢,这个方法可行。我一直在犯迷糊,以为需要加括号等等,但这个方法完美解决了! :) - HurkNburkS
1
如果您需要向用户显示此内容,请勿使用字符串格式。相反,使用NSNumberFormatter。这将根据用户的语言环境进行格式化。 - rmaddy
快速而方便。这很有帮助。 - Alok C

18

对于Swift 3,以Mb为单位:

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

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

7

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


#1. 使用ByteCountFormatterstring(fromByteCount:countStyle:)类方法

以下示例代码显示如何实现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. 使用 ByteCountFormatterstring(fromByteCount:) 方法

下面的示例代码展示了如何实现ByteCountFormatterstring(fromByteCount:)方法来将文件大小从字节手动转换为兆字节:

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. 使用ByteCountFormatterstring(from:countStyle:)类方法和Measurement

下面的示例代码展示了如何实现string(from: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 byteSize = Measurement(value: Double(byteCount), unit: UnitInformationStorage.bytes)
let displaySize = ByteCountFormatter.string(from: byteSize, countStyle: .file)
print(displaySize) // prints: 2.6 MB

#4. 使用 ByteCountFormatterstring(from:) 方法和 Measurement

下面的示例代码展示了如何使用 ByteCountFormatterstring(from:) 方法和 Measurement,通过手动将字节转换为兆字节来打印文件大小:

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

#5. 使用 MeasurementFormatterstring(from:) 方法和 Measurement

下面的示例代码展示了如何使用 MeasurementMeasurementFormatterstring(from:) 方法,将字节手动转换为兆字节以打印文件大小:

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 convertedSize = byteSize.converted(to: .megabytes)
let formatter = MeasurementFormatter()
let displaySize = formatter.string(from: convertedSize)
print(displaySize) // prints: 2.637 MB

如何获取以Double值表示的兆字节大小而不是MB? - user1553381
@user1553381,您可以将ByteCountFormatterincludesUnit属性设置为false,以仅包含计数。 - Imanou Petit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接