操纵 URL——最好的方法
我有一个图像 URL,我需要从中获取图像。
但在访问 URL 之前,我需要对其进行操作。例如:
URL :- http://www.myimages.com/XYZ?wid=50&hei=55&fmt=jpeg&qlt=95&op_sharpen=0&resMode=bicub
我从服务器接收此 URL 。我怎样才能最好地使用这些参数,将更新后的值附加回它们,然后转到新建的 URL?
I have a image URL from which I need to fetch the images.
But before hitting the URL I need to manipulate it. For example:
URL :- http://www.myimages.com/XYZ?wid=50&hei=55&fmt=jpeg&qlt=95&op_sharpen=0&resMode=bicub
I am receiving this URL from a server. How can I best play with these parameters, append them back with updated values, and the go to the newly-built URL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要替换 get 参数,您可以将该 URL 字符串存储到可变字符串中,然后使用
&yourvar=
或?yourvar=
模式查找>rangeOfString 然后确定该变量值的结束位置并替换该值。或者,我可能会使用它来分割它
comps = [URLstring elementsSeparatedByString:@"?"]
,然后 getParams = [NSMutableArray arrayWithArray:[[comps objectAtIndex:1] ComponentsSeparatedByString:@"&"]]
最后迭代 getParams 和 var = [NSMutableArray arrayWithArray:[[getParams objectAtIndex:counter] ComponentsSeparatedByString:@"="]]。
第一步分为 URI 和 GET 参数。第二步拆分 GET 参数。第三步使您可以访问每个参数名称及其值。如果您想要修改的变量与您的搜索匹配,您可以修改它们。
当您结束每个处理步骤时,您必须使用
[var ComponentsJoinedByString:@"&"]
保存每个处理步骤的结果。If you need to replace the get parameters you could store that URL string into a mutable string, and then look for the
&yourvar=
or?yourvar=
patterns usingrangeOfString
and then identify where that variable value ends and replace that value.OR, I would probably split it using
comps = [URLstring componentsSeparatedByString:@"?"]
,then
getParams = [NSMutableArray arrayWithArray:[[comps objectAtIndex:1] componentsSeparatedByString:@"&"]]
and finally iterate over getParams and
var = [NSMutableArray arrayWithArray:[[getParams objectAtIndex:counter] componentsSeparatedByString:@"="]]
.The first step splits between the URI and the GET parameters. The second step splits the GET parameters. The third step gives you access to each of the parameter names and their values. You can modify the variables that you want to modify at this step if they match your search.
As you end each of the processing steps you are going to have to save the result for each of them using
[var componentsJoinedByString:@"&"]
for example.如果您的网址是固定的,那么您提出问题就很简单。
NSString * string = [NSString stringWithFormat: ...];
方法会有帮助。但在我看来,你的网址也不固定。这就是您发布问题的原因。
您可以尝试获取“=”和“&”的索引字符。然后,您可以使用 NSString 类对象方法来获取子字符串,您可以将其替换为您的值,这将再次是字符串。
或者您可以将字符串转换为 const char *,然后尝试操作它。
If your url is fixed, then its simple fo r you to ask the question.
NSString * string = [NSString stringWithFormat: ...];
method will help. But seems to me that your url is also not fixed. That being the reason for you posting the question.
You can try to get the index of "=" and "&" chars. Then you can use NSString Class object methods to get the substrings, which you can replace, by your values, which will be again strings.
or you can convert the string to const char * and then try to manipulate it.
如果你使用的是iOS7,你可以使用NSURLComponents,看这个例子:
其他例子:
但是使用NSURLComponents你可以做很多其他的事情,检查NSURLComponents的公共接口,官方文档还没有在线提供。
If you're using iOS7, you can use NSURLComponents, look this example:
Other examples:
But with NSURLComponents you can do meny other things, check public interface of NSURLComponents, the official documentation nothing is available online yet.