【问题标题】:convert NSData Length from bytes to megs将 NSData 长度从字节转换为兆
【发布时间】:2012-12-10 22:15:22
【问题描述】:

我正在尝试 NSLog 我的 NSData 对象的 meg 数,但是目前我只能通过使用获得字节数

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

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

2.00 兆

任何帮助将不胜感激。

【问题讨论】:

  • 我将如何格式化它以使其后显示两位小数。

标签: ios nsdata nslog


【解决方案1】:

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

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

请注意,这是一种简单的方法,无法真正适应低于 1,048,576 字节或高于 1,073,741,823 字节的字节大小。有关可以处理不同文件大小的更完整的解决方案,请参阅:ObjC/Cocoa class for converting size to human-readable string?

或者对于 OS X 10.8+ 和 iOS 6+

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

在斯威夫特中:

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

【讨论】:

  • 酷,感谢一堆有用的东西。我很困惑,以为我必须有括号等,但这很好用! :)
  • 如果您需要向用户显示此内容,请不要使用字符串格式。相反,使用 NSNumberFormatter。这将为用户的区域设置格式化。
  • 快手。很有帮助
【解决方案2】:

对于 Swift 3,以 Mb 为单位:

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

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

【讨论】:

    【解决方案3】:

    在 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
    

    【讨论】:

    • 如何在没有 MB 的情况下将 mega 的大小作为 Double 值?
    • @user1553381 您可以将ByteCountFormatterincludesUnit 属性设置为false,以便仅包含计数。
    猜你喜欢
    • 2011-01-22
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 2023-04-02
    • 2012-05-28
    • 1970-01-01
    相关资源
    最近更新 更多