Cache

Discussion in 'iOS Programming' started by yaniv92648, Feb 15, 2010.

  1. yaniv92648 macrumors member

    Joined:
    Oct 26, 2009
    #1
    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.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Look at the class reference for UIImage. It contains a class method that creates and returns a UIImage based on an NSData.
     
  3. yaniv92648 thread starter macrumors member

    Joined:
    Oct 26, 2009
    #3
    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.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    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.
     
  5. yaniv92648 thread starter macrumors member

    Joined:
    Oct 26, 2009
    #5
    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..
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    My apologies. Forgot that writeToFile: uses the Documents directory and imageNamed: reads from the app bundle.

    Are you dead-set on needing the cache?
     
  7. yaniv92648 thread starter macrumors member

    Joined:
    Oct 26, 2009
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Just be aware that if you are developing for OS 2.x, that imageNamed: is a notoriously bad memory citizen.
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    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.
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    But those methods don't cache the image object.
     
  11. yaniv92648 thread starter macrumors member

    Joined:
    Oct 26, 2009
    #11
    but...

    PhoneyDeveloper - The methods u offered 2 use 2 get the UIImage from the iPhone DOES NOT cache the UIImage.
     
  12. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #12
    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.
     

Share This Page