I'm curious as to whether there is a public or private API to let me learn how much RAM any given UIImage uses? Anybody know how to get its size reliably?
A UIImage's data will be stored as decompressed data in memory, so you can ignore the suggestions of checking the byte length of UIImagePNGRepresentation etc.
The easiest solution is to grab the CGImageRef and use this code:
- (size_t)byteSizeForImage:(UIImage *)image {
CGImageRef imageRef = image.CGImage;
size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
size_t bitsTotal = CGImageGetWidth(imageRef) * CGImageGetHeight(imageRef) *
bitsPerPixel;
size_t bytesTotal = bitsTotal / 8;
return bytesTotal;
}
Most of the time your images will be RGB (3 bytes per pixel) or RGBA (4 bytes per pixel) so this usually works out to be width * height * 3 or width * height * 4.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With