我将这段代码用于图片的网址,我的网址没有问题,它包含\ 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/