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 8704603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:08:57+00:00 2026-06-13T03:08:57+00:00

I’ve written this bit of code to process a URL in string form to

  • 0

I’ve written this bit of code to process a URL in string form to try and fetch retina image of possible. The intent is that is turns

scheme://host/path/name.extension

into

scheme://host/path/name@2x.extension

The URL is predictable so assuming, for example, there is just one ‘.’ in the final filename is safe. But I have that iOS feeling, where I’m wondering if this is too much code to get this done; is there a better, maybe API-provided way to do this?

This is just the beginning of a method that kicks off the request.

- (NSData *)fetchResourceWithURLLocation:(NSString *)location
                             requestedBy:(id<DataRequestDelegate>)requester
                          withIdentifier:(id)requestID {

    NSData *resultData = nil;

    // some disassembly to get rid of the double '/' in scheme
    NSURL *locationURL = [NSURL URLWithString:location];
    NSString *host = [locationURL host];
    NSString *scheme = [locationURL scheme];
    NSString *dataPath = [locationURL relativePath];

    // if this is a retina display, rewrite the dataPath to use @2x
    if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0)) {

        NSArray *pathComponents = [dataPath componentsSeparatedByString:@"/"];
        NSString *nameComponent = [pathComponents lastObject];
        NSArray *nameComponents = [nameComponent componentsSeparatedByString:@"."];
        NSAssert(([nameComponents count] == 2), @"nameComponent contains more than one \".\"");
        nameComponent = [NSString stringWithFormat:@"%@@2x.%@", [nameComponents objectAtIndex:0], [nameComponents lastObject]];
        dataPath = @"";
        for(NSString *pathComponent in pathComponents) {
            if(pathComponent != [pathComponents lastObject])
                dataPath = [dataPath stringByAppendingString:[NSString stringWithFormat:@"%@/", pathComponent]];
        }
        dataPath = [dataPath stringByAppendingString:nameComponent];
    }

    // reassembly to check existence
    NSString *processedLocation = [NSString stringWithFormat:@"%@://%@%@", scheme, host, dataPath];
  • 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-13T03:08:58+00:00Added an answer on June 13, 2026 at 3:08 am

    You are right to think that is a dramatic amount of code to insert a string into your address string. What you want to do is fairly straightforward, so here is an example method that just pops the "@2x" into a string just before the last ".". It returns nil if there is no ".". Further explanation in code comments.

    -(NSString *)retinaURLStringForString:(NSString *)nonRetinaAddress{
        // Find the range (location and length of ".")
        // Use options parameter to start from the back.
        NSRange extDotRange = [nonRetinaAddress rangeOfString:@"." options:NSBackwardsSearch];
        // You can check whether the "." is there or not like this:
        if (extDotRange.location == NSNotFound){
            // Handle trouble
            return nil;
        }
    
        // We can use NSString's stringByReplacingCharactersInRange:withString: method to insert the "@2x".
        // To do this we first calculate the range to 'replace'.
        // For location we use the location of the ".".
        // We use 0 for length since we do not want to replace anything.
        NSRange insertRange = NSMakeRange(extDotRange.location, 0);
    
        // Lastly simply use the stringByReplacingCharactersInRange:withString: method to insert "@2x" in the insert range.
        NSString *retinaAddress = [nonRetinaAddress stringByReplacingCharactersInRange:insertRange withString:@"@2x"];
        return retinaAddress;
    }
    

    When tested like:

    NSString *nonRetinaAddress = @"scheme://host/path/name.extension";
    NSString *retinaAddress = [self retinaURLStringForString:nonRetinaAddress];
    
    NSLog(@"%@",nonRetinaAddress);
    NSLog(@"%@",retinaAddress);
    

    It logs:

    scheme://host/path/name.extension
    scheme://host/path/name@2x.extension
    

    Small side note:

    If the original string was a mutable string, you could use [mutableAddressString insertString:@"@2x" atIndex:extDotRange.location] instead of calculating a replacement range. Not worth creating a mutable for though.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,

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.