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

  • SEARCH
  • Home
  • 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 8699689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:00:41+00:00 2026-06-13T02:00:41+00:00

What would be a good way to pick cache file names for images downloaded

  • 0

What would be a good way to pick cache file names for images downloaded from internet for caching purpose to ensure that no matter what the URL is we got valid file name?

We often pick images from the net. It’s often useful to store those images in temporary cache so we don’t keep downloading the same image again and again.

So I suppose, I would need a cacheFileName. It should be a function of the URL. However, it must not contain special characters like :, /, &

In other words, it has to be a valid file name.

What would be the way to do so?

I can make some my self. Perhaps I can use URLEncode which seems to do just fine. However, I wonder, if there are consideration I missed.

This is the current Code I use:

- (NSString *) URLEncodedString {
    NSMutableString * output = [NSMutableString string];
    const char * source = [self UTF8String];
    int sourceLen = strlen(source);
    for (int i = 0; i < sourceLen; ++i) {
        const unsigned char thisChar = (const unsigned char)source[i];
        if (false && thisChar == ' '){
            [output appendString:@"+"];
        } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
                   (thisChar >= 'a' && thisChar <= 'z') ||
                   (thisChar >= 'A' && thisChar <= 'Z') ||
                   (thisChar >= '0' && thisChar <= '9')) {
            [output appendFormat:@"%c", thisChar];
        } else {
            [output appendFormat:@"%%%02X", thisChar];
        }
    }
    return output;
}

It’s taken from some great answer in stackOverflow which I have forgotten.

I wonder if the result of this function will always get a valid file name. Does anyone has better file name generator?

  • 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-13T02:00:42+00:00Added an answer on June 13, 2026 at 2:00 am

    If you do proper networking, NSURLCache handles caching of images just fine automatically.

    However, I got the feeling that you aren’t really interested in caching, but in (more or less permanently) storing images.

    In that case, build a proper model for your app.

    Because you don’t tell us what the images you want to be storing are for, I cannot be more helpful than this:

    If you’re on iOS 5+/OS X 10.7+, you can use NSAttributeDescription’s allowsExternalBinaryDataStorage setting to have the NSSQLiteStoreType handle naming, creating, retrieving, updating, and deleting the backing files for the image data transparently on your behalf.

    If you have to support older OS versions, you can still use external storage through a transient attribute. Your entity description then has to have an attribute for the name of the data-file, though. In that situation, the SHA-1 (available through the CommonCrypto framework) of the image’s data will provide for a good file name.


    Aside

    If you’ve found that code in an answer on Objective-C, downvote it, and delete that code above:

    -[NSString stringByAddingPercentEscapesUsingEncoding:], and CFURLCreateStringByAddingPercentEscapes do exactly what they (or their docs) say.

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

Sidebar

Related Questions

What would be a good way to determine if a string contains an IPv4
What would be a good way to writein C# a .NET3.5 Thread Safe DataSource
Wondering what the best / good way of doing this would be in jQuery.
What is a good way to represent finite automaton in Haskell? How would the
I've decided that it would be good for me to move outside of my
What would be a good approach in general to cache a web page where
I have some useful wpf buttons to test some functionality. It would be good
Two good examples would be google and facebook . I have lately pondered the
Question What would be a good (ideally, technical) reason to ever program some non-trivial
I would like to know if the following code would be a good pattern

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.