I'm trying to get the length of a UIImage
. Not the width or height of the image, but the size of the data.
12 Answers
UIImage *img = [UIImage imageNamed:@"sample.png"];
NSData *imgData = UIImageJPEGRepresentation(img, 1.0);
NSLog(@"Size of Image(bytes):%d",[imgData length]);

- 37,080
- 10
- 92
- 128

- 4,904
- 3
- 24
- 39
-
25Instead of 0 one should put 1.0; It's a quality of from original image. – Denis Kutlubaev Feb 25 '13 at 12:41
-
1what should be the right value passed for compression quality in UIImageJPEGRepresentation? 1.0 makes the calculated size bigger than the original size. – R3D3vil Jan 08 '14 at 17:04
-
1@Nisarg, 1.0 gives the highest quality and least compression. Try with 0.6 its a better quality with remarkable compression. – Meet Jan 10 '14 at 05:28
-
@Meet If you're trying to get the size of the image however you wouldn't want any compression or the result isn't going to be correct. – Jun 03 '15 at 09:44
-
1NSData *imgData = UIImageJPEGRepresentation(img, 1); – Fabio Jun 30 '15 at 18:48
-
5If the desire is to see how much memory the image is taking up, why would you want to get it's compressed size? And by using UIImageJPEGRepresentation, you are creating a new image, that occupies space and copies bits around; don't do this if you don't want more memory consumed or if you are squeezed for performance. – mahboudz May 17 '16 at 07:44
-
In Swift 3, length has been renamed to count: let imageData = UIImageJPEGRepresentation(img, 1.0) print("imageData: \(imageData?.count)") – Hellojeffy Oct 06 '16 at 19:19
The underlying data of a UIImage
can vary, so for the same "image" one can have varying sizes of data. One thing you can do is use UIImagePNGRepresentation
or UIImageJPEGRepresentation
to get the equivalent NSData
constructs for either, then check the size of that.
-
As we might expect, UIImageJPEGRepresentation may return half the size of when using UIImagePNGRepresentation – onmyway133 Jun 23 '16 at 07:38
-
7`UIImageJPEGRepresentation` and `UIImagePNGRepresentation` will not return the original size – NSLeader Feb 13 '18 at 07:29
Use the CGImage property of UIImage. Then using a combination of CGImageGetBytesPerRow *
CGImageGetHeight, add in the sizeof UIImage, you should be within a few bytes of the actual size.
This will return the size of the image, uncompressed, if you want to use it for purposes such as malloc in preparation for bitmap manipulation (assuming a 4 byte pixel format of 3 bytes for RGB and 1 for Alpha):
int height = image.size.height,
width = image.size.width;
int bytesPerRow = 4*width;
if (bytesPerRow % 16)
bytesPerRow = ((bytesPerRow / 16) + 1) * 16;
int dataSize = height*bytesPerRow;

- 39,196
- 16
- 97
- 124
-
4A couple problems here; (1) `image.size.height` and `image.size.width` will give you the size in CG units, and not necessarily pixels. If the `.scale` property isn't 1, you're going to get the wrong answer. (2) You are assuming 4 bytes per pixel, which is true for RGBA images but not for grayscale images. – Todd Lehman May 14 '16 at 00:08
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)editInfo
{
UIImage *image=[editInfo valueForKey:UIImagePickerControllerOriginalImage];
NSURL *imageURL=[editInfo valueForKey:UIImagePickerControllerReferenceURL];
__block long long realSize;
ALAssetsLibraryAssetForURLResultBlock resultBlock=^(ALAsset *asset)
{
ALAssetRepresentation *representation=[asset defaultRepresentation];
realSize=[representation size];
};
ALAssetsLibraryAccessFailureBlock failureBlock=^(NSError *error)
{
NSLog(@"%@", [error localizedDescription]);
};
if(imageURL)
{
ALAssetsLibrary *assetsLibrary=[[[ALAssetsLibrary alloc] init] autorelease];
[assetsLibrary assetForURL:imageURL resultBlock:resultBlock failureBlock:failureBlock];
}
}

- 6,381
- 5
- 25
- 30
-
1I think this is the more elegant solution when you deal with a UIImagePickerController – D33pN16h7 Sep 07 '12 at 18:51
-
1This is the best answer for working with UIIImagePickerController – Pawan Sharma Oct 03 '13 at 08:49
-
1Stunning Solution brother....Perfectly working...Everyone is advising to use JPEGRepresentation or PNGRepresentation...Those resulting incorrect image sizes...Thaks a lot.. You saved lot of my time. – SURESH SANKE Mar 22 '19 at 10:59
-
You have gave nice solution using imageURL to findout the image size. Is there any possibility to get it from the newly captured image using "image" as in the above code ? – SURESH SANKE Mar 22 '19 at 11:21
Example in Swift:
let img: UIImage? = UIImage(named: "yolo.png")
let imgData: NSData = UIImageJPEGRepresentation(img, 0)
println("Size of Image: \(imgData.length) bytes")

- 15,628
- 6
- 82
- 76
-
1If the desire is to see how much memory the image is taking up, why would you want to get it's compressed size? And by using UIImageJPEGRepresentation, you are creating a new image, that occupies space and copies bits around; don't do this if you don't want more memory consumed or if you are squeezed for performance. – mahboudz May 17 '16 at 07:44
This following is the fastest, cleanest, most general, and least error-prone way to get the answer. In a category UIImage+MemorySize
:
#import <objc/runtime.h>
- (size_t) memorySize
{
CGImageRef image = self.CGImage;
size_t instanceSize = class_getInstanceSize(self.class);
size_t pixmapSize = CGImageGetHeight(image) * CGImageGetBytesPerRow(image);
size_t totalSize = instanceSize + pixmapSize;
return totalSize;
}
Or if you only want the actual bitmap and not the UIImage instance container, then it is truly as simple as this:
- (size_t) memorySize
{
return CGImageGetHeight(self.CGImage) * CGImageGetBytesPerRow(self.CGImage);
}

- 280
- 2
- 17

- 2,880
- 1
- 26
- 32
Swift 3:
let image = UIImage(named: "example.jpg")
if let data = UIImageJPEGRepresentation(image, 1.0) {
print("Size: \(data.count) bytes")
}

- 7,315
- 6
- 30
- 38
Swift 4 & 5:
extension UIImage {
var sizeInBytes: Int {
guard let cgImage = self.cgImage else {
// This won't work for CIImage-based UIImages
assertionFailure()
return 0
}
return cgImage.bytesPerRow * cgImage.height
}
}

- 3,194
- 1
- 27
- 35
I'm not sure your situation. If you need the actual byte size, I don't think you do that. You can use UIImagePNGRepresentation or UIImageJPEGRepresentation to get an NSData object of compressed data of the image.
I think you want to get the actual size of uncompressed image(pixels data). You need to convert UIImage* or CGImageRef to raw data. This is an example of converting UIImage to IplImage(from OpenCV). You just need to allocate enough memory and pass the pointer to CGBitmapContextCreate's first arg.
UIImage *image = //Your image
CGImageRef imageRef = image.CGImage;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
IplImage *iplimage = cvCreateImage(cvSize(image.size.width, image.size.height), IPL_DEPTH_8U, 4);
CGContextRef contextRef = CGBitmapContextCreate(iplimage->imageData, iplimage->width, iplimage->height,
iplimage->depth, iplimage->widthStep,
colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault);
CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), imageRef);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);
IplImage *ret = cvCreateImage(cvGetSize(iplimage), IPL_DEPTH_8U, 3);
cvCvtColor(iplimage, ret, CV_RGBA2BGR);
cvReleaseImage(&iplimage);

- 1,113
- 8
- 9
SWIFT 4+
let imgData = image?.jpegData(compressionQuality: 1.0)
debugPrint("Size of Image: \(imgData!.count) bytes")
you can use this trick to find out image size.

- 1,685
- 22
- 37
If needed in human readable form we can use ByteCountFormatter
if let data = UIImageJPEGRepresentation(image, 1.0) {
let fileSizeStr = ByteCountFormatter.string(fromByteCount: Int64(data.count), countStyle: ByteCountFormatter.CountStyle.memory)
print(fileSizeStr)
}
Where Int64(data.count)
is what you need in numeric format.

- 13,264
- 3
- 57
- 82
I tried to get image size using
let imgData = image.jpegData(compressionQuality: 1.0)
but it gives less than the actual size of image. Then i tried to get size using PNG representation.
let imageData = image.pngData()
but it gives more byte counts than the actual image size.
The only thing that worked perfectly for me.
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var asset: PHAsset!
if #available(iOS 11.0, *) {
asset = info[UIImagePickerControllerPHAsset] as? PHAsset
} else {
if let url = info[UIImagePickerControllerReferenceURL] as? URL {
asset = PHAsset.fetchAssets(withALAssetURLs: [url], options: .none).firstObject!
}
}
if #available(iOS 13, *) {
PHImageManager.default().requestImageDataAndOrientation(for: asset, options: .none) { data, string, orien, info in
let imgData = NSData(data:data!)
var imageSize: Int = imgData.count
print("actual size of image in KB: %f ", Double(imageSize) / 1024.0)
}
} else {
PHImageManager.default().requestImageData(for: asset, options: .none) { data, string, orientation, info in
let imgData = NSData(data:data!)
var imageSize: Int = imgData.count
print("actual size of image in KB: %f ", Double(imageSize) / 1024.0)
}
}
}

- 801
- 8
- 27