我已经写了这一点代码来处理字符串形式的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];
您可以认为这是将字符串插入地址字符串中的巨大代码。您想做的是相当简单的,因此,这是一个示例方法,它只是将"@2x"
弹出到最后一个"."
之前。如果没有"."
,它将返回零。代码注释中的进一步说明。
-(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]
而不是计算替换范围。