操纵 URL——最好的方法

发布于 2024-10-27 03:51:36 字数 380 浏览 4 评论 0原文

我有一个图像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天赋异禀 2024-11-03 03:51:36

如果您需要替换 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 using rangeOfString 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.

远山浅 2024-11-03 03:51:36

如果您的网址是固定的,那么您提出问题就很简单。

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.

感情废物 2024-11-03 03:51:36

如果你使用的是iOS7,你可以使用NSURLComponents,看这个例子:

NSString *urlString = @"https://mail.google.com/mail/u/0";
NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlString];
[components setQuery:@"shva=1"];

其他例子:

//Example One
NSString *urlString = @"https://mail.google.com/mail/u/0/?shva=1#inbox";
NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlString];

NSLog(@"%@ - %@ - %@ - %@", components.scheme, components.host, components.query, components.fragment);

//Example Two
NSString *urlString = @"https://mail.google.com/mail/u/0/?shva=1#inbox";
NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlString];

if (components) {
    //good URL
} else {
    //bad URL
}

//Example three
NSURLComponents *components = [NSURLComponents new];
[components setScheme:@"https"];
[components setHost:@"mail.google.com"];
[components setQuery:@"shva=1"];
[components setFragment:@"inbox"];
[components setPath:@"/mail/u/0/"];

[self.webview loadRequest:[[NSURLRequest alloc] initWithURL:[components URL]]];

但是使用NSURLComponents你可以做很多其他的事情,检查NSURLComponents的公共接口,官方文档还没有在线提供。

If you're using iOS7, you can use NSURLComponents, look this example:

NSString *urlString = @"https://mail.google.com/mail/u/0";
NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlString];
[components setQuery:@"shva=1"];

Other examples:

//Example One
NSString *urlString = @"https://mail.google.com/mail/u/0/?shva=1#inbox";
NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlString];

NSLog(@"%@ - %@ - %@ - %@", components.scheme, components.host, components.query, components.fragment);

//Example Two
NSString *urlString = @"https://mail.google.com/mail/u/0/?shva=1#inbox";
NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlString];

if (components) {
    //good URL
} else {
    //bad URL
}

//Example three
NSURLComponents *components = [NSURLComponents new];
[components setScheme:@"https"];
[components setHost:@"mail.google.com"];
[components setQuery:@"shva=1"];
[components setFragment:@"inbox"];
[components setPath:@"/mail/u/0/"];

[self.webview loadRequest:[[NSURLRequest alloc] initWithURL:[components URL]]];

But with NSURLComponents you can do meny other things, check public interface of NSURLComponents, the official documentation nothing is available online yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文