Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

daproject85

macrumors member
Original poster
Apr 13, 2011
37
0
Hi Forum,

I currently have a NSData object from a URL. The code is as follows:

Code:
NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];

now I would like to cache this image file and for that I would have to get the path via NSFileManager...etc etc..Then eventually use the method

Code:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
from NSData class

now this image is a JPEG...so what is the difference between the following.

Code:
NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
[data writeToFile: uniquePath atomically: YES];

and

Code:
NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
UIImage *image = [[UIImage alloc] initWithData: data];
[UIImageJPEGRepresentation(image, 100) writeToFile: uniquePath atomically: YES];

is there a difference between whats in the NSDATA object "data" and the NSData object that the function from UIKIT: NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);
returns??
 

xStep

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
Use NSData's isEqualToData: to test what you read from the url and what gets returned from the UIImageJPEGRepresentation function.
 

Reason077

macrumors 68040
Aug 14, 2007
3,607
3,644
yes they are different...but which one is correct?

Do you want to make sure to save/cache the image exactly as it appears on the web? If so, use the first one.

If you want to convert everything to JPG (which may involve re-compressing the image, which can degrade quality), then use the second one.

I'm guessing you probably want the first.
 

daproject85

macrumors member
Original poster
Apr 13, 2011
37
0
Do you want to make sure to save/cache the image exactly as it appears on the web? If so, use the first one.

If you want to convert everything to JPG (which may involve re-compressing the image, which can degrade quality), then use the second one.

I'm guessing you probably want the first.


Reasons thanks for you reply... i noticed the following: the first method creates a file which is 767KB (same size at the jpeg file ). the second method creates a file which is about 2.7 MB. The original file is a JPEG. So now it leaves me confused as to why the second method creates a files thats bigger. But yes to answer your question I want the file exactly how it appeared in my UIImageView

----------

There's also SDWebImage for async loading / caching.

Dejo, what is the significance of using an asyn loading/caching? Basically im asking whats so special about doing it "asynchronously" vs synchronously and what would an example of synchronous caching be?
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Dejo, what is the significance of using an asyn loading/caching? Basically im asking whats so special about doing it "asynchronously" vs synchronously and what would an example of synchronous caching be?

The async only applies to the loading and not the caching. Doesn't really make sense it that context. Anyways, asynchronous loading means that the main thread (which is where all UI is handled) is not blocked and therefore the image can be loaded "in the background". Therefore, the UX is never hung-up waiting for an image to load for a remote source.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
It's a well known property of jpeg image files that if you compress them repeatedly each time the image data will change. Also, if you have a jpeg that's been saved at say 40% and then you take that image and save it at 100% the new file will be larger although it will probably look the same as the 40% version.

There's no reason to re-compress a jpeg to a higher % compression. And if you want it to remain the same then don't change it at all.
 

daproject85

macrumors member
Original poster
Apr 13, 2011
37
0
It's a well known property of jpeg image files that if you compress them repeatedly each time the image data will change. Also, if you have a jpeg that's been saved at say 40% and then you take that image and save it at 100% the new file will be larger although it will probably look the same as the 40% version.

There's no reason to re-compress a jpeg to a higher % compression. And if you want it to remain the same then don't change it at all.

that made perfect sense thanks!! so the UIImageJPEGRepresentation is for more of a granular operation.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.