将 NSData 长度从字节转换为兆

问题描述 投票:0回答:3

我正在尝试

NSLog
我的
NSData
对象的兆数,但是目前我只能通过使用

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

那么我该如何更改这个

NSLog
声明,以便我可以看到类似

的内容

2.00 兆

如有任何帮助,我们将不胜感激。

ios nsdata nslog
3个回答
128
投票

1 KB 为 1000 个字节,1 MB 为 1000 KB,所以...

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

请注意,这是一种简单的方法,不能处理特别大或特别小的数字。更通用的解决方案是使用 Apple 的 NSByteCountFormatter:

或适用于 OS X 10.8+ 和 iOS 6+

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

在斯威夫特:

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

17
投票

对于 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。使用
ByteCountFormatter
string(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。使用
ByteCountFormatter
string(fromByteCount:)
方法

以下示例代码展示了如何实现

ByteCountFormatter
string(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。使用
ByteCountFormatter
string(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。使用
ByteCountFormatter
string(from:)
方法和
Measurement

以下示例代码展示了如何实现

ByteCountFormatter
string(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。使用
MeasurementFormatter
string(from:)
方法和
Measurement

以下示例代码展示了如何实现

Measurement
MeasurementFormatter
string(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
© www.soinside.com 2019 - 2024. All rights reserved.