将NSData Length从字节转换为megs

Hur*_*rkS 36 nslog nsdata ios

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

NSLog(@"%u", myData.length);
Run Code Online (Sandbox Code Playgroud)

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

2.00兆

任何帮助,将不胜感激.

Mic*_*lum 118

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

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

请注意,这是一种简单的方法,无法真正适应低于1,048,576字节或高于1,073,741,823字节的字节大小.有关可以处理不同文件大小的更完整的解决方案,请参阅:用于将大小转换为人类可读字符串的ObjC/Cocoa类?

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

NSLog(@"%@", [[NSByteCountFormatter new] stringFromByteCount:data.length]);
Run Code Online (Sandbox Code Playgroud)

在Swift中:

print(ByteCountFormatter().string(fromByteCount: Int64(data.count)))
Run Code Online (Sandbox Code Playgroud)


小智 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)")
Run Code Online (Sandbox Code Playgroud)


Ima*_*tit 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
Run Code Online (Sandbox Code Playgroud)

#2. 使用ByteCountFormatterstring(fromByteCount:)方法

以下示例代码展示了如何实现ByteCountFormatter'sstring(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
Run Code Online (Sandbox Code Playgroud)

#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
Run Code Online (Sandbox Code Playgroud)

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

以下示例代码显示了如何实现ByteCountFormatter'sstring(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
Run Code Online (Sandbox Code Playgroud)

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

以下示例代码展示了如何实现Measurementand ,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
Run Code Online (Sandbox Code Playgroud)