Domanda

Sto pianificando di cache le immagini da un server e utilizzare mostrarla come slide show di ordinamento nella mia app.Sarei caricando asincrono le immagini.

Ho due opzioni:

    .
  1. o per memorizzare in cache le immagini come un file e usarla quando necessario.
  2. Cache Le immagini oggetti in memoria e usarlo quando mai è necessario e scriverlo in file quando l'applicazione è uscita.

    Quale sarebbe meglio?

    Per favore fammi sapere se hai qualche tipo di suggerimento per quanto riguarda le immagini nella cache.

È stato utile?

Soluzione

Il tuo secondo approccio ha 2 difetti importanti:

    .
  1. Se ci sono troppe immagini, la tua applicazione otterrà un avviso di memoria basso e dovrai disporre le tue immagini dalla memoria comunque
  2. Non è anche una buona idea salvare tutte le immagini da file su richiesta dell'applicazione - non è garantito che il tuo codice di salvataggio si terminerà sull'uscita dell'applicazione (ad esempio se ci vuole un sistema troppo lungo potrebbe semplicemente terminare la tua app e le tue immagini sarannoperso)

    Suggerirei di salvare le immagini ai file subito dopo averle scaricato e conservare in memoria il numero ragionevole di immagini che devi mostrare senza ritardo visibile (caricando immagini aggiuntive quando richiesto e smaltimento non necessario)

Altri suggerimenti

I would recommend you the first option. Leaves you more flexibility, e.g. when the data size increases the memory size.

I'd do it like this: Have a NSMutableDictionary with the cached images (as UIImage objects). If the image is not in the cache, look whether it's available as a file. If it's not available as a file, load it, put it into your dictionary and also write it to a file.

As for where to write the files to: you can either use the NSTemporaryDirectory() or create a directory inside your NSLibraryDirectory (use NSSearchPathForDirectoriesInDomains to locate it). The later has the advantage/disadvantage that it will be in the iTunes backup (whether that's an advantage or not depends on the use case). Using the Library directory is Apple's recommended way of storing data that is backed up but does not appear in the iTune's file exchange thingy (Documents directory).

I have started using EGOImageView to handle my caching; it's very versatile and handles the intricacies of caching for you.

It works very well for pulling images via http, you can find it on the EGO developer website here

http://developers.enormego.com/

For image caching solution on iOS platform, you might want to consider SDWebImage framework available at: https://github.com/rs/SDWebImage. It is very easy to integrate and takes care of all your image caching worries.: read more about the working here: https://github.com/rs/SDWebImage#readme

We recently picked this up for our app and it works great.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top