0

我编写了这段代码来处理字符串形式的 URL,以尝试获取可能的视网膜图像。意图是转弯

scheme://host/path/name.extension

进入

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

URL 是可预测的,因此假设例如只有一个“.”。在最终文件名中是安全的。但是我有那种 iOS 的感觉,我想知道这是否需要太多的代码来完成;有没有更好的,也许是 API 提供的方法来做到这一点?

这只是启动请求的方法的开始。

- (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];
4

1 回答 1

1

您认为将字符串插入地址字符串的代码量非常大,这是对的。你想做的事情相当简单,所以这里有一个示例方法,它"@2x"只是在 last 之前将 the 弹出到一个字符串中"."。如果没有,则返回 nil "."。代码注释中的进一步解释。

-(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;
}

当测试如下:

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

NSLog(@"%@",nonRetinaAddress);
NSLog(@"%@",retinaAddress);

它记录:

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

小旁注:

如果原始字符串是可变字符串,则可以使用[mutableAddressString insertString:@"@2x" atIndex:extDotRange.location]而不是计算替换范围。不值得为此创建一个可变的。

于 2012-10-21T03:31:04.283 回答