2014-09-12 66 views
-1

我想創建UIImageView與鏈接的圖像。我不知道如何從後臺線程鏈接加載圖像。我已經使用第三方庫,如SDWebImage,這是非常好的。 但我想知道如何做到這一點沒有任何第三方庫。 請幫忙!用鏈接創建UIImageView圖像?

THanks提前回答!

+0

檢查'sendAsynchronousRequest:隊列:completionHandler:'的'方法NSURLConnection' – Desdenova 2014-09-12 11:10:57

+0

你的建議是明顯的,但如果有從不同的URL加載圖像的數量,那麼我應該怎麼辦? – 2014-09-12 11:18:55

回答

0

這實際上很可能沒有任何第三方庫,試試這個。

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 100)]; 
NSData *imageData; 
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
      dispatch_async(concurrentQueue, ^{ 
       // async thread 
       imageData=[NSData dataWithContentsOfURL:pictureUrl]; 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        // return to main thread 
        imageView.image=[[UIImage alloc]initWithData:imageData]; 
       }); 
      }); 
+0

謝謝M大衛! – 2014-09-12 11:22:26