Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8987371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:46:28+00:00 2026-06-15T21:46:28+00:00

Hello I would like to run a thread and check the current downloaded size

  • 0

Hello I would like to run a thread and check the current downloaded size of a file.

This is what I use

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://lasp.colorado.edu/home/wp-content/uploads/2011/03/suncombo1.jpg"]]];

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *jpegFilePath = [NSString stringWithFormat:@"%@/test.jpeg",docDir];
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
[data2 writeToFile:jpegFilePath atomically:YES];
downloadStatus.text =[NSString stringWithFormat:@"size: %zd", malloc_size(data2)];

[image release];

I have also tried to change malloc_size(data2) into image but again it is not the real result. I know this does not have thread and do not check during the download process but what am I supposed to use here to see the file size?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:46:29+00:00Added an answer on June 15, 2026 at 9:46 pm

    A couple of observations:

    1. Your question presumed that your attempts to retrieve the size of the NSData were failing. They are not. The correct way to get the size of a NSData is via length.

    2. Your confusion, though, stems from a faulty assumption that taking an externally generated JPEG on a roundtrip through UIImage and UIImageJPEGRepresentation would yield the identical NSData. This would have been extraordinarily unlikely. There are too many different JPG settings that could have changed (see the JPEG Wikipedia page). We certainly don’t know what settings that original file used. I know that UIImage and/or UIImageJPEGRepresentation changed the color space of the file. I’d wager it’s doing a lot of other things, too.

    3. So your results are correct. The original file was 2.6mb and the resulting file was 4.5mb. If you change the compressionQuality from 1.0 to 0.99, the resulting file is only 1.4mb! But if you want the original file, just save it first (like I do below).

    Consider the following code which downloads the image file, saves it, loads it into a UIImage, re-extracts it via UIImageJPEGRepresentation, and saves another copy of the image:

    // let's make filenames where we'll store the files
    
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *suncomboOrig = [documentsPath stringByAppendingPathExtension:@"suncombo1-orig.jpg"];
    NSString *suncomboReprocessed = [documentsPath stringByAppendingPathExtension:@"suncombo1-reprocessed.jpg"];
    
    // let's download the original suncombo1.jpg and save it in Documents and display the size
    
    NSURL *url = [NSURL URLWithString:@"http://lasp.colorado.edu/home/wp-content/uploads/2011/03/suncombo1.jpg"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSLog(@"original = %d", [data length]);
    [data writeToFile:suncomboOrig atomically:NO];
    
    // let's load that into a UIImage
    
    UIImage *image = [UIImage imageWithData:data];
    
    // let's extract data out of the image and write that to Documents, too, also logging the size of that
    
    NSData *data2 = UIImageJPEGRepresentation(image, 1.0);
    NSLog(@"reprocessed = %d", [data2 length]);
    [data2 writeToFile:suncomboReprocessed atomically:NO];
    

    What that does is it reports:

    2012-12-13 22:30:39.576 imageapp[90647:c07] original = 2569128
    2012-12-13 22:30:40.141 imageapp[90647:c07] reprocessed = 4382876
    

    So the first file I saved (which I suspect is identical to what’s on your server) was 2.5mb, and the file after doing a roundtrip to a UIImage and re-extracted via 4.3mb. If I look at the two files that the above code saved, I can confirm that these NSData sizes are correct.


    My original answer was predicated on the presumption that the OP was either unable to retrieve the size of a NSData or that there was some subtle issue underlying the simple question (such as wanting to get the size before the download commenced). Anyway, I’ve expanded my answer above, but I’ll keep my original answer for historical purposes:

    Original Answer:

    The NSData property length tells you how many bytes were downloaded. E.g. [data2 length].

    If it’s really big, you can use NSURLConnection to download it asynchronously, which, depending upon your web server, may provide total file size before the download commences in the method didReceiveResponse (with the expectedContentLength property in the NSHTTPURLResponse *response parameter).

    The other nice thing about NSURLConnection downloading is that you don’t have to load the entire file in memory as you’re downloading it, but rather you can stream it directly to persistent storage, which is especially useful if you’re downloading multiple large files at the same time. If you’re downloading a reasonably sized file, using NSURLConnection to download is overkill, but it can be nice when downloading large files and you want a progress indicator (or want to get the file size before the download commences).

    But if you just want to know how many bytes were downloaded to your NSData, use length.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I would like to use preg_match in PHP to parse the Desired text
Hello I would like to use the sass module in a play application. We
I created a file called Hello.java that looks like this: public class Hello {
In c, we create a thread like so: void * run(void * arg){ printf(hello
Hello I would like to display an other splashscreen after the default one, if
hello i would like to know if there is any dll or global hook
Hello I would like to know if my script is good; I want to
Hello I have a datatable that I would like to filter with a single
In the following code I would like to hide Hello world but keep visible
Hello! I would like to extract all citations from a text. Additionally, the name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.