Get size of a UIImage (bytes length) not height and width

后端 未结 10 2139
说谎
说谎 2020-11-28 09:00

I\'m trying to get the length of a UIImage. Not the width or height of the image, but the size of the data.

10条回答
  •  天命终不由人
    2020-11-28 09:45

    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);
    }
    

提交回复
热议问题