0

When sharing an image in an iPhone, we are given an opportunity to pick different sizes of an image - Small, Medium, Large, Original. And an opportunity of see the size in KB/MB along with each classification.

  1. Where does apple expose this code for users to leverage? I couldn't spot it in my searches in iOS docs.
  2. What are some tested & tried frameworks/methods available on GitHub or elsewhere that can emulate this behavior?

EDITED: I evaluated the solution posted by Brad Larson: UIImage: Resize, then Crop By confirming a decrease in size via the size measuring solution given below. Together they are a good fit.

Community
  • 1
  • 1
pulkitsinghal
  • 3,855
  • 13
  • 45
  • 84

1 Answers1

1
  1. Where does apple expose this code for users to leverage? I couldn't spot it in my searches in iOS docs.

iOS is not open-source project. Apple releases only sample code which is used like tutorial sample project.


Here's an article regarding calcutation of image file size.

This is very simple, though. Here's the code:

UIImage *image = [UIImage imageNamed:@"your_big_image.png"];

size_t depth = CGImageGetBitsPerPixel(image.CGImage);
size_t width = CGImageGetWidth(image.CGImage);
size_t height = CGImageGetHeight(image.CGImage);

double bytes = ((double)width * (double)height * (double)depth) / 8.0;

Now you have your image size in bytes.

To convert it to kB divide it by 1024. If you want to get MB, divide by 1048576 (1024x1024).

double kb = bytes / 1024.0f;
double mb = bytes / 1048576.0f

Then you can display it to your user by formatting your message as following:

NSString *msg = [NSString stringWithFormat:@"Original %dx%d (%.2f MB)", (int)width,(int)height, (float)mb];
bneely
  • 9,083
  • 4
  • 38
  • 46
akashivskyy
  • 44,342
  • 16
  • 106
  • 116