2

Is there a way to get the filesize in KB from a UIImage, without getting that image from didFinishPickingMediaWithInfo? The images that are presented are coming from the photo album.

I tried the following code, but this gives the following result: size of image in KB: 0.000000

- (void)setImage:(UIImage *)image
{
    _image = image;
    self.imageView.image = image;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];

        self.backgroundColor = [UIColor whiteColor];

        panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(beingDragged:)];
        [self addGestureRecognizer:panGestureRecognizer];

        // prepare image view
        self.imageView = [[UIImageView alloc]initWithFrame:self.bounds];
        self.imageView.clipsToBounds = YES;
        self.imageView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:self.imageView];



        NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((_image), 0.5)];
        int imageSize = imgData.length;
        NSLog(@"size of image in KB: %f ", imageSize/1024.0);

        overlayView = [[OverlayView alloc]initWithFrame:CGRectMake(self.frame.size.width/2-100, 0, 100, 100)];
        overlayView.alpha = 0;
        [self addSubview:overlayView];


    }
    return self;
}
Jan
  • 611
  • 1
  • 7
  • 18

3 Answers3

4

Here is an example of calculating file sizes of files in your HomeDirectory or Documents:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"yourimagename.png"]

File sie is calculated:

       filesize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil] fileSize];

       NSLog(@"%lld",filesize);

Before you do that add filesize, you can add it in the .m file

@interface ViewController () {
long long filesize;
}

this will result in bytes, if you are trying to convert those bytes into kb you can use the NSByteCountFormatter and it will take care of all the math for you:

 NSByteCountFormatter *sizeFormatter = [[NSByteCountFormatter alloc] init];
sizeFormatter.countStyle = NSByteCountFormatterCountStyleFile;

and then call it like so :

[sizeFormatter stringFromByteCount:filesize]

If the image is not saved on the disk you can calculate the size this way:

NSData *imgData = UIImageJPEGRepresentation(_image, 1);
filesize = [imgData length]; //filesize in this case will be an int not long long so use %d to NSLog it
soulshined
  • 7,480
  • 4
  • 31
  • 63
  • How do I get the attributesOfItemAtPath of a UIImage? – Jan Dec 11 '14 at 21:40
  • @Jan if you don't want to store it on disk you can cache it and just get the cache directory. The way you want to achieve it is a way of doing it yes, but harmful because your essentially creating a duplicate picture just to obtain the file size. – soulshined Dec 11 '14 at 21:56
  • @Jan you could also try this to simply see if you can obtain the filesize if you can't do that then there are issues elsewhere: NSLog(@"size of image in KB: %i",[UIImagePNGRepresentation(_image) length]); – soulshined Dec 11 '14 at 21:59
  • can you please take a look at this so you know what I mean http://stackoverflow.com/questions/27453713/how-to-get-date-and-filesize-from-a-picture Thanks! – Jan Dec 12 '14 at 23:34
  • @Jan so is your picture on disk or not? – soulshined Dec 13 '14 at 01:06
  • Yes the image is on disk – Jan Dec 13 '14 at 10:13
  • @Jan okay so your in good shape. the code provided is correct. is it not working for you? or do you not know how to get to the path of your image? – soulshined Dec 13 '14 at 10:14
  • I do not know how to get the path of that image, and how can I make this a string? Thnx! – Jan Dec 13 '14 at 10:17
  • @Jan it's really hard to tell, i don't know where you store your pictures in the bundle, but provided you store them in the Documents folder the revised code will do the trick. other than that i can't help you out. Happy coding - – soulshined Dec 13 '14 at 10:26
1

initWithFrame: runs before setImage: is called, so _image is nil at the time you are doing your calculations. You could move them into the setImage: function...

However, this is a weird way of measuring the size of the image. A JPEG file and what ends up in graphics memory are widely different, so if you are doing it for profiling reasons, this is not going to give you any accurate measurements. If you just want the size on disk, you can simply check that through NSFileManager.

JustSid
  • 24,711
  • 7
  • 72
  • 97
  • Thank you for your answer, because I can see indeed the filesize is incorrect. Can you show me how to do it with NSFileManager? – Jan Dec 11 '14 at 20:55
  • @Jan The following SO question (and answer) should help you: http://stackoverflow.com/questions/5743856/finding-files-size – JustSid Dec 11 '14 at 21:02
  • Well, that didn't helped me much JustSid, as I try to retrieve the date from a UIImage instead of a file with url. – Jan Dec 11 '14 at 21:17
  • @Jan I'm sorry to tell you, but this information is not retained in the `UIImage`. Can I ask what you are actually trying to solve here, before I'm just throwing around random ideas of what you could do? – JustSid Dec 12 '14 at 00:03
  • can you please take a look at this so you know what I mean.. http://stackoverflow.com/questions/27453713/how-to-get-date-and-filesize-from-a-picture Thank you. – Jan Dec 12 '14 at 23:30
0
NSInteger imageDataSize = CGImageGetHeight(image.CGImage) * CGImageGetBytesPerRow(image.CGImage);
strox
  • 23
  • 2