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

yaniv92648

macrumors member
Original poster
Oct 26, 2009
96
0
Hi,
i have an image in a url which i wanna take and save to the iphone.
i would like to load the image later from the cache.
i saved the image from the url to the iphone (couldn't find a way to save the image as a UIImage so i saved the image as NSData), i wanna get the image by UIImage imageNamed (in order to get cache) but i can't cuz i have to load NSData. i can get the file from the iphone with NSData dataWithContentOfFile but then i won't take it from cache..
any help will be appreciated..
Thanks.
 
cache

not good because after i save it for the first time on the iphone, all other times i wanna load it from the cache.
 
Ah, then you should look at the class reference for NSData and see if there's a method to write it to a file. Once it's in the file, you should be able to use imageNamed: to load it via caching.
 
still no good

i already did it, but u can't save an NSData to the iphone and then take a UIImage... i don't think u read my initial post, i went through all of these solutions and nothing worked..
 
My apologies. Forgot that writeToFile: uses the Documents directory and imageNamed: reads from the app bundle.

Are you dead-set on needing the cache?
 
You can't use imageNamed for images that you download. It loads images from your app's bundle and you can't write to your app's bundle.

You should probably save the images as png or jpeg. Use UIImagePNGRepresentation() and then write out the resulting NSData. Then you can load the png file with UIImage imageWithContentsOfFile or imageWithData.
 
but...

PhoneyDeveloper - The methods u offered 2 use 2 get the UIImage from the iPhone DOES NOT cache the UIImage.
 
I'm aware that those methods don't cache the image. But imageNamed isn't available for images outside the app bundle. So there's no way to use the built-in image cache with images that you download.

What is the goal? Better memory usage or better performance?

imageNamed keeps its own cache of images. It should be simple to write your own cache that loads images, keeps them in memory, and responds to memory warnings by unloading all the images. If you like you could write an LRU cache that unloads images based on whether they've been used recently or not.

imageNamed also has some kind of performance improvement for images that I don't understand. It's not related to the cache. It uses the underlying CG methods to get better performance in a way that's been discussed on the Apple iPhone developer's list but you'll have to look it up there if you want to duplicate that.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.