I\'m trying to get the length of a UIImage. Not the width or height of the image, but the size of the data.
This following is the fastest, cleanest, most general, and least error-prone way to get the answer. In a category UIImage+MemorySize:
#import
- (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);
}