Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache URL images iphone UITableview

I'm seeking a tutorial on how to cache images loaded from a url into cells of a uitableview.

I found an example here

http://www.ericd.net/2009/05/iphone-caching-images-in-memory.html#top

But the code is incomplete. I'm an objective c novice so I found it very difficult to fill in the missing pieces.

like image 531
dubbeat Avatar asked Feb 15 '10 10:02

dubbeat


People also ask

How to clear cache on Safari on iPhone?

To clear the cache in Safari: Tap the Settings app on the iPhone home screen. Tap Safari. Tap Clear History and Website Data. In the confirmation box, tap Clear History and Data (or tap Cancel if you change your mind).

What is a cache on an iPhone?

The iPhone creates a cache whenever you use it. The files are created by apps and contain data the apps use when they run. The cache for a web browser contains temporary files. That's one common kind of cache, but all sorts of apps and the iPhone's operating system also have caches.

How do I clear the third-party app cache on my iPhone?

The settings to clear caches for some third-party apps are located in the iPhone's Settings app. For example, to clear the cache of the Accuweather app: Tap the iPhone's Settings app. Scroll down and tap the AccuWeather app. Turn on the Reset cached content slider.

How do I clear the cache on my browser?

To clear the Safari cache: Go to Settings > Safari > Clear History and Website Data. For other browsers, clear cache in the app's settings. To clear the cache from third-party apps: Navigate to the app within the iOS Settings app and toggle Reset cached content.


1 Answers

Here is a simple ImageCache implementation using NSCache. ImageCache is a singelton.

ImageCache.h

    #import <Foundation/Foundation.h>

    @interface ImageCache : NSObject

    @property (nonatomic, retain) NSCache *imgCache;


    #pragma mark - Methods

    + (ImageCache*)sharedImageCache;
    //- (void) AddImage:(NSString *)imageURL: (UIImage *)image;
   - (void) AddImage:(NSString *)imageURL withImage:(UIImage *)image;
    - (UIImage*) GetImage:(NSString *)imageURL;
    - (BOOL) DoesExist:(NSString *)imageURL;

    @end

ImageCache.m

  #import "ImageCache.h"

    @implementation ImageCache

    @synthesize imgCache;

    #pragma mark - Methods

    static ImageCache* sharedImageCache = nil;

    +(ImageCache*)sharedImageCache
    {
        @synchronized([ImageCache class])
        {
            if (!sharedImageCache)
                sharedImageCache= [[self alloc] init];

            return sharedImageCache;
        }

        return nil;
    }

    +(id)alloc
    {
        @synchronized([ImageCache class])
        {
            NSAssert(sharedImageCache == nil, @"Attempted to allocate a second instance of a singleton.");
            sharedImageCache = [super alloc];

            return sharedImageCache;
        }

        return nil;
    }

    -(id)init 
    {
        self = [super init];
        if (self != nil) 
        {
            imgCache = [[NSCache alloc] init];
        }

        return self;
    }

   // - (void) AddImage:(NSString *)imageURL: (UIImage *)image
- (void) AddImage:(NSString *)imageURL withImage:(UIImage *)image
    {
        [imgCache setObject:image forKey:imageURL];
    }

    - (NSString*) GetImage:(NSString *)imageURL
    {
        return [imgCache objectForKey:imageURL];
    }

    - (BOOL) DoesExist:(NSString *)imageURL
    {
        if ([imgCache objectForKey:imageURL] == nil)
        {
            return false;
        }

        return true;
    }


    @end

Example

UIImage *image;

    // 1. Check the image cache to see if the image already exists. If so, then use it. If not, then download it.

    if ([[ImageCache sharedImageCache] DoesExist:imgUrl] == true)
    {
        image = [[ImageCache sharedImageCache] GetImage:imgUrl];
    }
    else
    {
        NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: imgUrl]];
        image = [[UIImage alloc] initWithData:imageData];

        // Add the image to the cache 
        //[[ImageCache sharedImageCache] AddImage:imgUrl :image];

        [[ImageCache sharedImageCache] AddImage:imgUrl withImage:image];
    }
like image 61
Flea Avatar answered Sep 21 '22 01:09

Flea