A few observations:
In your question, it was assumed that your attempts to extract the NSData size failed. They are not. The correct way to get the size of an NSData is through length .
Your confusion, however, stems from the erroneous assumption that getting externally created JPEGs backwards through UIImage and UIImageJPEGRepresentation will result in an identical NSData . That would be incredibly unlikely. There are too many different JPG settings that could change (see the Wikipedia JPEG page ). Of course, we donβt know which settings use the original file. I know that UIImage and / or UIImageJPEGRepresentation have changed the color space of the file. I would bet on doing a lot of other things.
So your results are correct. The source file was 2.6 MB, and the resulting file was 4.5 MB. If you change the compressionQuality value from 1.0 to 0.99, the resulting file will be 1.4 MB! But if you want the source file, save it first (for example, I do below).
Consider the following code, which downloads an image file, saves it, loads it into UIImage , retrieves it through UIImageJPEGRepresentation and saves another copy of the image:
What does this mean, he 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
Thus, the first file I saved (which I suspect is identical to the one on your server) was 2.5 MB, and the file after completing the transition to UIImage and re-extracted after 4.3 MB. If I look at the two files that are saved above, I can confirm that these NSData sizes are correct.
My initial answer was based on the assumption that the OP either could not get the NSData size, or there was some kind of subtle problem underlying the simple question (for example, if you want to get the size before loading), Anyway, I expanded my answer above, but I will keep my original answer for historical purposes:
Original answer:
The NSData length property tells you how many bytes were downloaded. For instance. [data2 length] .
If it is really large, you can use NSURLConnection to download it asynchronously, which depending on your web server may provide a shared file before starting the download in the didReceiveResponse method (with expectedContentLength in the NSHTTPURLResponse *response parameter).
Another nice thing about downloading NSURLConnection is that you do not need to load the entire file into memory when you download it, but you can transfer it directly to the persistent storage, which is especially useful if you are "Downloading multiple large files at once." If you are uploading a file with a sufficiently large size, using NSURLConnection to upload is redundant, but may be good when uploading large files, and you want a progress indicator (or want to get the file size before downloading).
But if you just want to know how many bytes have been downloaded to your NSData , use length .