2014-10-28 29 views
-1

提取使用URL的圖像我有一個UIView,我試圖從URL獲取一些數據,如以下,如何在iOS的

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat: @"www.url"; 
    // Create a NSURLRequest with the given URL 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 

     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                   cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData 
                  timeoutInterval:30.0]; 
     [request setValue:@"03f0d561-e173-4296-ab07-0ba301ca56bb" forHTTPHeaderField:@"token"]; 

     // Get the data 
     NSURLResponse *response = nil; 
     NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 
     // Now create a NSDictionary from the JSON data 
     if (data != nil) { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 


       jsonDataDictionary = [[jsonDictionary objectForKey:@"data"] objectAtIndex:0]; 

      }); 
     } 
    }); 

,當我運行此代碼,圖像是黑色的。圖片沒有加載。誰能幫我這個。

在此先感謝!

+0

請問您的網址包含方案(例如,「HTTP://」)?這是必要的。記錄'NSURL'並確保它不是'nil'。另外,你說它不工作,但是你沒有使用NSURLConnection或NSJSONSerialization中的任何'NSError'參數來告訴你爲什麼失敗。你應該使用那些'NSError'參數,如果失敗就記錄它們。否則,你是盲目的。 – Rob 2014-10-28 03:33:44

回答

0

您發佈的代碼存在許多問題。首先,你應該使用

+ (void)sendAsynchronousRequest:(NSURLRequest *)request 
          queue:(NSOperationQueue *)queue 
       completionHandler:(void (^)(NSURLResponse *response, 
            NSData *data, 
       NSError *connectionError))handler 

而不是使用主線程的同步請求。無論如何,問題源於你如何處理數據。你試圖用NSJSONSerialization解析它。這不是JSON。它的圖像數據(推測在.png或.jpg中)。

您需要使用

+ (UIImage *)imageWithData:(NSData *)data 

如果這仍然不起作用,複製URL粘貼到瀏覽器,看看它是什麼MIME類型

0

輕鬆就可以使用網址提取圖像。如果你做下面的代碼很容易獲取。

這裏非常非常重要的事情是從服務器

-> First you have to convert to NSData 

    -> After that you have to convert to image. 

下面的編碼部分獲取的圖像時

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL  URLWithString:@"http://www.url"]]; 

    [request setHTTPMethod:@"GET"]; 

    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"]; 

    NSError *err; 

    NSURLResponse *response; 

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 

    //You need to check response.Once you get the response copy that and paste in ONLINE JSON VIEWER in Google.If you do this clearly you can get the correct results. 

    //After that it depends upon the json format whether it is DICTIONARY or ARRAY 

    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err]; 

    //Now if you have IMAGE URL in Dictionary 

    //Also you can use valueForKey instead of objectForKey for getting images from server 

    NSString *stringImage = [NSString stringWithFormat:@"%@",[jsonDict objectForKey:@"Data"]objectForKey:@"ImageURL"]; 

        OR 
    //Now if you have image URL in Array of Dictionary 

    NSMutableArray *array = [jsonDict objectForKey:@"Data"]; 

    for(int i=0;i<[array count];i++) 
    { 

     NSString *stringImage = [NSString stringWithFormat:@"%@",[[array objectAtIndex:i]objectForKey:@"ImageURL"]]; 

    } 

    // Now Convert into Data from String 

    NSData *data = [NSData dataWithContentsOfURL:[NSURL urlWithString:stringImage]]; 

    // Now convert into Image from Data 

    yourImageView.image = [UIImage imageWithData:data];