38

NSLogオブジェクトのメガ数を試していNSDataますが、現在取得できるのは、を使用してバイト数だけです。

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

NSLogでは、このステートメントをどのように変更して、次のようなものが表示されるようにしますか?

2.00メガ

どんな助けでもいただければ幸いです。

4

3 に答える 3

124

キロバイトには10​​24バイト、メガバイトには10​​24キロバイトがあるので...

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

これは単純なアプローチであり、1,048,576バイト未満または1,073,741,823バイトを超えるバイトサイズには実際には適切に対応できませんでした。さまざまなファイルサイズを処理できるより完全なソリューションについては、次を参照してください。サイズを人間が読める文字列に変換するためのObjC /Cocoaクラス?

または、OSX10.8以降およびiOS6以降の場合

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

Swiftの場合:

print(ByteCountFormatter().string(fromByteCount: Int64(data.count)))
于 2012-12-10T22:21:35.293 に答える
16

Swift 3の場合、Mbで:

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

print("File size: \(fileSize)")
于 2017-04-25T20:17:39.430 に答える
4

Swift5.1とiOS13では、次の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:)方法を使用する

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。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

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。MeasurementFormatterstring(from:)方法を使用してMeasurement

次のサンプルコードは、バイトをメガバイトに手動で変換してファイルサイズを出力するためのMeasurementMeasurementFormatterの実装方法を示しています。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
于 2019-06-24T18:19:46.813 に答える