convert NSData Length from bytes to megs convert NSData Length from bytes to megs ios ios

convert NSData Length from bytes to megs


There are 1024 bytes in a kilobyte and 1024 kilobytes in a megabyte, so...

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?

Or for OS X 10.8+ and iOS 6+

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

In Swift:

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


For Swift 3, in Mb:

let countBytes = ByteCountFormatter()countBytes.allowedUnits = [.useMB]countBytes.countStyle = .filelet fileSize = countBytes.string(fromByteCount: Int64(dataToMeasure!.count))print("File size: \(fileSize)")


With Swift 5.1 and iOS 13, you can use one of the 5 following ways to solve your problem.


#1. Using ByteCountFormatter's string(fromByteCount:countStyle:) class method

The following sample code shows how to implement string(fromByteCount:countStyle:) in order to print a file size by automatically converting bytes to a more appropriate storage unit (e.g. megabytes):

import Foundationlet url = Bundle.main.url(forResource: "image", withExtension: "png")!let data = try! Data(contentsOf: url)let byteCount = data.countprint(byteCount) // prints: 2636725let displaySize = ByteCountFormatter.string(fromByteCount: Int64(byteCount), countStyle: .file)print(displaySize) // prints: 2.6 MB

#2. Using ByteCountFormatter's string(fromByteCount:) method

The following sample code shows how to implement ByteCountFormatter's string(fromByteCount:) in order to print a file size by manually converting bytes to megabytes:

import Foundationlet url = Bundle.main.url(forResource: "image", withExtension: "png")!let data = try! Data(contentsOf: url)let byteCount = data.countprint(byteCount) // prints: 2636725let formatter = ByteCountFormatter()formatter.allowedUnits = [.useMB]formatter.countStyle = .filelet displaySize = formatter.string(fromByteCount: Int64(byteCount))print(displaySize) // prints: 2.6 MB

#3. Using ByteCountFormatter's string(from:countStyle:) class method and Measurement

The following sample code shows how to implement string(from:countStyle:) in order to print a file size by automatically converting bytes to a more appropriate storage unit (e.g. megabytes):

import Foundationlet url = Bundle.main.url(forResource: "image", withExtension: "png")!let data = try! Data(contentsOf: url)let byteCount = data.countprint(byteCount) // prints: 2636725let 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

The following sample code shows how to implement ByteCountFormatter's string(from:) and Measurement in order to print a file size by manually converting bytes to megabytes:

import Foundationlet url = Bundle.main.url(forResource: "image", withExtension: "png")!let data = try! Data(contentsOf: url)let byteCount = data.countprint(byteCount) // prints: 2636725let byteSize = Measurement(value: Double(byteCount), unit: UnitInformationStorage.bytes)let formatter = ByteCountFormatter()formatter.allowedUnits = [.useMB]formatter.countStyle = .filelet displaySize = formatter.string(from: byteSize)print(displaySize) // prints: 2.6 MB

#5. Using MeasurementFormatter's string(from:) method and Measurement

The following sample code shows how to implement Measurement and MeasurementFormatter's string(from:) in order to print a file size by manually converting bytes to megabytes:

import Foundationlet url = Bundle.main.url(forResource: "image", withExtension: "png")!let data = try! Data(contentsOf: url)let byteCount = data.countprint(byteCount) // prints: 2636725let 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