Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9066195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:41:04+00:00 2026-06-16T16:41:04+00:00

I am implementing a Cache in my iOS app, that would keep images downloaded

  • 0

I am implementing a Cache in my iOS app, that would keep images downloaded in RAM.

I did some research and found some code but most of them were for caching images to permanent storage.

I tried NSCache but couldn’t work it around for my need.

The requirements are:

  • Limit on saving images. e.g. 100.
  • As the Cache limit is reached, it should remove most older image inserted before adding a new one.

I’m not sure about the exact word but I think it should be called FIFO cache (First in first out).

After some research, I did the following implementation.

static NSMutableDictionary *thumbnailImagesCache = nil;

+ (UIImage *)imageWithURL:(NSString *)_imageURL
{
    if (thumbnailImagesCache == nil) {
        thumbnailImagesCache = [NSMutableDictionary dictionary];
    }

    UIImage *image = nil;

    if ((image = [thumbnailImagesCache objectForKey:_imageURL])) {
        DLog(@"image found in Cache")
        return image;
    }

    /*  the image was not found in cache - object sending request for image is responsible to download image and save it to cache */
    DLog(@"image not found in cache")
    return nil;
}

+ (void)saveImageForURL:(UIImage *)_image URLString:(NSString *)_urlString
{
    if (thumbnailImagesCache == nil) {
        thumbnailImagesCache = [NSMutableDictionary dictionary];
    }

    if (_image && _urlString) {
        DLog(@"adding image to cache")

        if (thumbnailImagesCache.count > 100) {
            NSArray *keys = [thumbnailImagesCache allKeys];
            NSString *key0 = [keys objectAtIndex:0];
            [thumbnailImagesCache removeObjectForKey:key0];
        }

        [thumbnailImagesCache setObject:_image forKey:_urlString];
        DLog(@"images count in cache = %d", thumbnailImagesCache.count)
    }
}

Now the problem is that I’m not sure weather this is the correct/efficient solution. Any one have any better idea/solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:41:06+00:00Added an answer on June 16, 2026 at 4:41 pm

    Your assumption about the order of the keys is certainly incorrect. The order of the keys in an NSDictionary is unspecified, the key and value at index 0 need not be the oldest one. You shall store the creation date of each image in the method where you put them in the cache dictionary.

    Apart from that, the rest of the code seems valid.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing an image cache system for caching downloaded image. My strategy is based
I am implementing a simple cache using LinkedHashMap based on the instructions found here
I am implementing a Twitter app, where the app needs to cache details of
Basically, I am implementing own cache system. Ideally, it'll look like this: $CACHE->start($name); //CODE
I have some places where implementing some sort of cache might be useful. For
I'm implementing a memory cache for a table that looks like this (simplified): Item1
I am currently implementing a custom c# cache provider that I can plug in
While implementing Multithreaded Cache, i found this article on MSDN. http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx This is for
I'm implementing an image cache for PNGs, downloaded from the web. Works well so
folks! I'm implementing a shared cache in my app. The idea is to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.