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?
A couple of observations:
Your question presumed that your attempts to retrieve the size of the
NSDatawere failing. They are not. The correct way to get the size of aNSDatais vialength.Your confusion, though, stems from a faulty assumption that taking an externally generated JPEG on a roundtrip through
UIImageandUIImageJPEGRepresentationwould yield the identicalNSData. 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 thatUIImageand/orUIImageJPEGRepresentationchanged the color space of the file. I’d wager it’s doing a lot of other things, too.So your results are correct. The original file was 2.6mb and the resulting file was 4.5mb. If you change the
compressionQualityfrom 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 viaUIImageJPEGRepresentation, and saves another copy of the image:What that does is it reports:
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
UIImageand re-extracted via 4.3mb. If I look at the two files that the above code saved, I can confirm that theseNSDatasizes are correct.My original answer was predicated on the presumption that the OP was either unable to retrieve the size of a
NSDataor 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
NSDatapropertylengthtells you how many bytes were downloaded. E.g.[data2 length].If it’s really big, you can use
NSURLConnectionto download it asynchronously, which, depending upon your web server, may provide total file size before the download commences in the methoddidReceiveResponse(with theexpectedContentLengthproperty in theNSHTTPURLResponse *responseparameter).The other nice thing about
NSURLConnectiondownloading 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, usingNSURLConnectionto 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, uselength.