Jonathan Thurft
Jonathan Thurft

Reputation: 4173

Does a NSData image weight in memory as much as the opened image?

I am doing an upload function to my server from my iOS app. I am right now architecting the best way to do this and thinking that my approach would run into trouble.

I've over 20 pictures that I would like to send in 1 request using AFmultiparFormData in AFNetworking with also text information that is relevant to that picture.

Now... I know how to create the NSDictionary with lots of information inside but never created a NSDictionary with 20 images in NSData format or attempted to upload many pictures at once.

The issue is that I don't know whether the NSData images would take as much memory space as an image. Thus, I could only do 2/3 at the time (if they were full screen images which they are not, but its just an arbitrary point of measurement)

Given What I said and the seudo code below, Could I do that or would I ran out of memory? otherwise I would be left to do 1 by 1.

// for loop that does this 20 times
UIImage* image = [UIImage imageNamed:myExercise.exercisePictureThumb];
NSData * exercisePictureThumb = UIImagePNGRepresentation(image1);

// add to dictionary recursively

Upvotes: 2

Views: 487

Answers (2)

Rob
Rob

Reputation: 438232

MishieMoo is absolutely right regarding the memory/streaming considerations and the wise counsel to avoid loading all of the images into memory at one time.

I wanted to get back to your UIImage vs NSData question, though. In my mind, there are two considerations:

  1. File size: Unfortunately, there is no simple answer as to whether the original image will be smaller than the UIImage/UIImagePNGRepresentation output. Sometimes, if the original file was a JPEG, the result of round-tripping the image through a UIImage can actually make the file larger (e.g. I just grabbed three images and the PNG roundtrip took them from 4.7mb to 38.9mb). But, if the originals were uncompressed images, then the PNG roundtrip could make them considerably smaller.

  2. Data loss: For me, the more significant issue with round-tripping the image through UIImage is that you'll suffer data loss. You lose meta data. Depending upon the image, you can even lose image data in the translation, too (e.g. if original wasn't in sRGB color space, if bit-depth lowered, etc.).

Now, sometimes it makes perfect sense (e.g., I use a variation of this UIImage round trip technique when creating image thumbnails). But if my intent is to upload images onto a server, I'd be very wary about going through the UIImage conversion, possibly losing data, without a compelling business case.

Upvotes: 1

MishieMoo
MishieMoo

Reputation: 6680

You should not put all of your image data into a NSDictionary, as that's likely to cause memory problems.

Instead, create a dictionary or array of the URLs to your files, then make use of AFNetworking's multipartFormRequestWithMethod:path:parameters:constructingBodyWithBlock: and the AFMultipartFormData protocol, particularly appendPartWithFileURL:name:fileName:mimeType:error:. This way your image data for all of the images is not all in memory at one time, but is instead streamed from disk. Huge performance increase.

Upvotes: 5

Related Questions