2014-09-29 140 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]; 

使用此代碼的行在您的代碼中進行了註釋。