ios - 处理NSURL中的特殊字符

标签 ios afnetworking nsurl

我将这段代码用于图片的网址,我的网址没有问题,它包含\ u,xcode认为它是特殊字符,因此我通过在网址中添加\ u进行转义
但是当我通过它

fullPath = [NSString stringWithFormat:@"http:\\www.school-link.net\\uploads\\%@",image_url];
    NSLog(@"file path  %@",fullPath);
    //i try escape space by this code it dosnt work 
   //fullPath = [fullPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [[NSURL alloc]initWithString:fullPath];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];



 AFHTTPRequestOperation *posterOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    posterOperation.responseSerializer = [AFImageResponseSerializer serializer];
    [posterOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Response: %@", responseObject);
        image_view.image = responseObject;

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Image request failed with error: %@", error);
    }];
    [[NSOperationQueue mainQueue] addOperation:posterOperation];
    [posterOperation start];

它给我错误,任何想法
谢谢你们

图像请求失败,并显示以下错误:错误域= NSURLErrorDomain代码= -1000“错误的URL” UserInfo = 0x8f9f900 {NSUnderlyingError = 0x9a94cf0“错误的URL”,NSLocalizedDescription =错误的URL}

最佳答案

您的网址格式不正确。现在您有:

http:\\www.school-link.net\\uploads\\%@

这些反斜杠应改为正斜杠。例如:
http://www.school-link.net/uploads/%@

现在,您可以将image_url附加到URL,并且假定image_url不会导致URL格式错误,您的请求将通过。

关于ios - 处理NSURL中的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26103417/

相关文章:

ios - 给定一个 podspec 文件,我该怎么做才能将它包含在我的项目中?

objective-c - NSBundle 文件引用在应用程序重新安装后丢失

ios - 容器 View 和传递数据

ios - Advanced NSOperation - 在运行时添加依赖

ios - 从ASIHTTPRequest到AFNetworking

objective-c - NSURL 书签 : A Faster Alternative?

objective-c - NSURL比较: compare with different URL

iphone - 解码任意 block NSData?

ios - 无法访问 4 天天气 - MSN 天气

ios - 我可以在每次调用方法时将同名的 UIImage 添加到 NSMutableArray 中吗?