Finding out the size of the uploaded image file in iOS

Hello, I would like to start a stream and check the current loaded file size.

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 also tried changing malloc_size (data2) to an image, but again this is not a real result. I know that this one does not have a thread and is not checked during the download process, but what should I use here to see the file size?

+4
source share
3 answers

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:

 // let 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 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 load that into a UIImage UIImage *image = [UIImage imageWithData:data]; // let 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 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 .

+5
source

You can simply use the C FILE class to get the file size.

  FILE * handle = fopen([jpegFilePath UTF8String], "r"); fseek(handle, EOF); // seek to end of file int size = ftell(handle); // returns position in bytes fclose(); 
+4
source

Since you say "current" size and mention the stream, I assume that you are trying to determine the size of the file as it is received. In this case, you can get the stream for free from NSURLConnection , and you can get the size of the data from the delegate methods as it gets ...

Create an instance variable for the loaded data:

 @property (nonatomic, strong) NSMutableData *data; 

Create and run the connection:

 NSURL *url = [NSURL URLWithString:@"http://lasp.colorado.edu/home/wp-content/uploads/2011/03/suncombo1.jpg"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; self.data = [NSMutableData data]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

Deploy the required methods to NSURLConnectionDataDelegate . For your question, the special part:

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.data appendData:data]; NSUInteger bytesSoFar = [self.data length]; // you're on the app main thread here, so you can do something // to the UI to indicate progress downloadStatus.text = [NSString stringWithFormat@ "size: %d", bytesSoFar]; } 

Here is a good document in the rest of the protocol . When the connection is complete, you can create the image in the same way as using dataWithContentsOfURL ...

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { UIImage *image = [[UIImage alloc] initWithData:self.data]; downloadStatus.text = [NSString stringWithFormat@ "size: %d", [self.data length]]; } 
+3
source

All Articles