Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 4, 2012, 11:22 PM   #1
daproject85
macrumors member
 
Join Date: Apr 2011
caching UIImage

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??
daproject85 is offline   0 Reply With Quote
Old Nov 5, 2012, 01:18 AM   #2
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: Lost in Minneapolis
Use NSData's isEqualToData: to test what you read from the url and what gets returned from the UIImageJPEGRepresentation function.
__________________
My App: CameraTime - Time lapse photography for novice and advanced users.
xStep is offline   0 Reply With Quote
Old Nov 5, 2012, 01:22 AM   #3
daproject85
Thread Starter
macrumors member
 
Join Date: Apr 2011
yes they are different...but which one is correct?
daproject85 is offline   0 Reply With Quote
Old Nov 5, 2012, 02:36 AM   #4
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: Lost in Minneapolis
Try displaying both of them.
xStep is offline   0 Reply With Quote
Old Nov 5, 2012, 02:40 AM   #5
daproject85
Thread Starter
macrumors member
 
Join Date: Apr 2011
like writing them back to NSdata and making a UIImage out of them?
daproject85 is offline   0 Reply With Quote
Old Nov 5, 2012, 06:24 AM   #6
Reason077
macrumors 65816
 
Join Date: Aug 2007
Quote:
Originally Posted by darealjakesh View Post
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.
Reason077 is offline   0 Reply With Quote
Old Nov 5, 2012, 07:08 AM   #7
jnoxx
macrumors 65816
 
jnoxx's Avatar
 
Join Date: Dec 2010
Location: Aartselaar // Antwerp // Belgium
You could use the AsyncImageView from NickLockWood which does all of this for you + it's async..
__________________

iPad Mini, iPad 4, iPad 2, iPhone 3G,4,5, iMac 24", Mac Mini Last gen, Macbook Pro Retina with Dell U2711
jnoxx is offline   0 Reply With Quote
Old Nov 5, 2012, 08:28 AM   #8
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
There's also SDWebImage for async loading / caching.
__________________
dejo is offline   0 Reply With Quote
Old Nov 5, 2012, 02:43 PM   #9
daproject85
Thread Starter
macrumors member
 
Join Date: Apr 2011
Quote:
Originally Posted by Reason077 View Post
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

----------

Quote:
Originally Posted by dejo View Post
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?
daproject85 is offline   0 Reply With Quote
Old Nov 5, 2012, 04:11 PM   #10
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by daproject85 View Post
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.
__________________
dejo is offline   0 Reply With Quote
Old Nov 5, 2012, 05:24 PM   #11
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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.
PhoneyDeveloper is offline   0 Reply With Quote
Old Nov 5, 2012, 05:43 PM   #12
daproject85
Thread Starter
macrumors member
 
Join Date: Apr 2011
Quote:
Originally Posted by PhoneyDeveloper View Post
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.
daproject85 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Caching Service IscariotJ Mac OS X Server, Xserve, and Networking 4 Dec 29, 2013 04:35 PM
Caching - not working ^squirrel^ Mac OS X Server, Xserve, and Networking 3 Oct 30, 2013 07:46 AM
UIImage Effects xArtx iPhone/iPad Programming 12 Jun 21, 2013 09:11 PM
Work with uiimage Flexoid iPhone/iPad Programming 4 Jan 24, 2013 02:36 PM
Software Update Caching NSNick Mac OS X Server, Xserve, and Networking 1 Nov 14, 2012 02:27 PM

Forum Jump

All times are GMT -5. The time now is 05:32 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC