ios 将 NSData 长度从字节转换为兆字节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13810204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert NSData Length from bytes to megs
提问by HurkNburkS
I am trying to NSLog
the number of megs my NSData
object is however currently all I can get is bytes by using
我正在尝试NSLog
我的NSData
对象的兆字节数,但是目前我只能通过使用获得字节数
NSLog(@"%u", myData.length);
So how would I change this NSLog
statement so I can see something like
那么我将如何更改此NSLog
声明,以便我可以看到类似的内容
2.00 megs
2.00 兆
any help would be appreciated.
任何帮助,将不胜感激。
回答by Mick MacCallum
There are 1024 bytes in a kilobyte and 1024 kilobytes in a megabyte, so...
一千字节有 1024 字节,一兆字节有 1024 千字节,所以......
NSLog(@"File size is : %.2f MB",(float)myData.length/1024.0f/1024.0f);
Mind you, this is a simplistic approach that couldn't really properly accommodate for byte sizes below 1,048,576 bytes or above 1,073,741,823 bytes. For a more complete solution that can handle varying file sizes, see: ObjC/Cocoa class for converting size to human-readable string?
请注意,这是一种简单的方法,无法真正适应低于 1,048,576 字节或高于 1,073,741,823 字节的字节大小。有关可以处理不同文件大小的更完整的解决方案,请参阅:ObjC/Cocoa 类用于将大小转换为人类可读的字符串?
Or for OS X 10.8+ and iOS 6+
或用于 OS X 10.8+ 和 iOS 6+
NSLog(@"%@", [[NSByteCountFormatter new] stringFromByteCount:data.length]);
In Swift:
在斯威夫特:
print(ByteCountFormatter().string(fromByteCount: Int64(data.count)))
回答by Ber.to
For Swift 3, in Mb:
对于 Swift 3,以 Mb 为单位:
let countBytes = ByteCountFormatter()
countBytes.allowedUnits = [.useMB]
countBytes.countStyle = .file
let fileSize = countBytes.string(fromByteCount: Int64(dataToMeasure!.count))
print("File size: \(fileSize)")
回答by Imanou Petit
With Swift 5.1 and iOS 13, you can use one of the 5 following ways to solve your problem.
使用 Swift 5.1 和 iOS 13,您可以使用以下 5 种方法之一来解决您的问题。
#1. Using ByteCountFormatter
's string(fromByteCount:countStyle:)
class method
#1. usingByteCountFormatter
的string(fromByteCount:countStyle:)
类方法
The following sample code shows how to implement string(fromByteCount:countStyle:)
in order to print a file size by automaticallyconverting bytes to a more appropriate storage unit (e.g. megabytes):
以下示例代码显示了如何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. Using ByteCountFormatter
's string(fromByteCount:)
method
#2. usingByteCountFormatter
的string(fromByteCount:)
方法
The following sample code shows how to implement ByteCountFormatter
's string(fromByteCount:)
in order to print a file size by manuallyconverting bytes to megabytes:
以下示例代码显示了如何通过手动将字节转换为兆字节来实现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
#3. Using ByteCountFormatter
's string(from:countStyle:)
class method and Measurement
#3. usingByteCountFormatter
的string(from:countStyle:)
类方法和Measurement
The following sample code shows how to implement string(from:countStyle:)
in order to print a file size by automaticallyconverting bytes to a more appropriate storage unit (e.g. megabytes):
以下示例代码显示了如何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. Using ByteCountFormatter
's string(from:)
method and Measurement
#4. usingByteCountFormatter
的string(from:)
方法和Measurement
The following sample code shows how to implement ByteCountFormatter
's string(from:)
and Measurement
in order to print a file size by manuallyconverting bytes to megabytes:
以下示例代码显示了如何实现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
#5. Using MeasurementFormatter
's string(from:)
method and Measurement
#5. usingMeasurementFormatter
的string(from:)
方法和Measurement
The following sample code shows how to implement Measurement
and MeasurementFormatter
's string(from:)
in order to print a file size by manuallyconverting bytes to megabytes:
以下示例代码示出了如何实现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