有没有更好的方法来重写URL以获取@ 2x图像资源?

时间:2012-10-18 04:45:56

标签: iphone uiimage retina-display

我已经编写了这段代码来处理字符串形式的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];

1 个答案:

答案 0 :(得分:1)

您认为将字符串插入地址字符串是一个巨大的代码是正确的。你想要做的是相当简单的,所以这里有一个示例方法,只是将"@2x"弹出到最后一个"."之前的字符串中。如果没有".",则返回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]而不是计算替换范围。不值得创造一个可变的。