2014-09-29 106 views
1

我有这段代码的URL的形象,我有我的网址有小问题它包含\ u和xcode认为它是特殊字符所以我逃脱它通过添加\ u在我的url 但是当我通过它处理特殊字符在NSURL

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]; 

它给我的错误,任何想法 谢谢大家

image request failed with error: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x8f9f900 {NSUnderlyingError=0x9a94cf0 "bad URL", NSLocalizedDescription=bad URL}

回答

2

你的URL格式不正确。现在你有:

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

那些反斜杠应该是正斜杠。例如:

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

现在你可以追加image_url的URL,并且假定image_url不会导致URL被畸形的,您的要求会经过。

1

使用NSUTF8String编码。它会解决问题。例如

NSString *strURL = [NSString stringWithContentsOfURL:yourURL encoding:NSUTF8StringEncoding error:NULL]; 

使用此代码的行在您的代码中进行了注释。