2013-10-09 29 views
0
  • 我想從捲曲web服務的數據,但我的代碼是不工作我想分析從目標C捲曲基地web服務數據圖像和我使用的代碼

    NSURL *url = [NSURL URLWithString:delegate.profilePic]; 
         NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:picUrl]; 
        [req setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
         [req setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"]; 
         NSString *token =delegate.Token; 
         [req setValue:token forHTTPHeaderField:@"X-Auth-Token"]; 
        [req setHTTPMethod:@"GET"]; 
        NSError *error = nil; 
        NSURLResponse *response; 
        NSData *urlData=[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error]; 
        NSString *myString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]; 
         NSLog(@"Url Data %@",myString); 
    
+0

這是非常好的代碼!有什麼問題 ? –

+0

對於初學者,您錯過了這行代碼的末尾:'NSURL * picUrl = [NSURL URLWithString:@「'。請修復它,以便我們可以嘗試提供幫助。 – neilco

回答

0

試試這個代碼,我用它來獲取圖像數據

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
dispatch_async(queue, ^{ 
    NSURL *url = [NSURL URLWithString:delegate.profilePic]; 
    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url]; 
    [req setHTTPMethod:@"GET"]; 

    NSString *boundary = @"0x0hHai1CanHazB0undar135"; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [req addValue:contentType forHTTPHeaderField: @"Content-Type"]; 
    [req setValue:delegate.Token forHTTPHeaderField:@"X-Auth-Token"]; 
    NSError *error = nil; 
    NSURLResponse *response; 
    NSData *urlData=[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error]; 

    dispatch_sync(dispatch_get_main_queue(), ^{ 

     profilePicView.image=[UIImage imageWithData:urlData]; 
    }); 
}); 
相關問題