Advertisement
loonerz

Get Imgur image urls and display on CollectionView

Sep 29th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.07 KB | None | 0 0
  1.         // Makes a request to Imgur API
  2.         public void MakeRequest(){
  3.             var request = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/gallery/hot/viral/0.json");
  4.             request.Headers.Add ("Authorization", "Client-ID " + "920c7e989ed4deb");
  5.             request.Method = "GET";
  6.  
  7.             Task<WebResponse> task = Task.Factory.FromAsync (
  8.                                          request.BeginGetResponse,
  9.                                          asyncResult => request.EndGetResponse (asyncResult),
  10.                                          (object)null);
  11.             task.ContinueWith (t => ReadStreamFromResponse (t.Result));
  12.  
  13.         }
  14.         // Read the stream response, parse it to JSON, get the image urls ignoring the albums
  15.         private void ReadStreamFromResponse(WebResponse response){
  16.             using (Stream responseStream = response.GetResponseStream ()) {
  17.                 using (StreamReader sr = new StreamReader (responseStream)) {
  18.                     string content = sr.ReadToEnd ();
  19.                     var json = JsonObject.Parse (content);
  20.                     var array = json ["data"];
  21.  
  22.                     List<string> urls = new List<string> ();
  23.                     foreach (JsonObject o in array) {
  24.                         string url = o ["link"];
  25.                         bool isAlbum = o ["is_album"];
  26.                         if (!isAlbum) {
  27.                             url = url.Insert (url.Length - 4, "s");
  28.                             urls.Add (url);
  29.                         }
  30.                     }
  31.                     totalPhotos = urls.Count;
  32.  
  33.                     foreach (var url in urls) {
  34.                         AddElement (url);
  35.                     }
  36.                 }
  37.             }
  38.         }
  39.         // For each image url, get the data, place it in a UIImage and add it to collectionData,
  40.         // then assign it to the CollectionSource's photos List object and call ReloadData
  41.         public void AddElement(string url){
  42.  
  43.  
  44.             using (var imgUrl = new NSUrl (url)) {
  45.                 using (var data = NSData.FromUrl (imgUrl)) {
  46.                     collectionData.Add (UIImage.LoadFromData (data));
  47.                     downloadedPhotos++;
  48.                     if (downloadedPhotos == totalPhotos) {
  49.                         Console.WriteLine ("Finished downloading all photos");
  50.                         InvokeOnMainThread (delegate {
  51.                             photoSource.photos = collectionData;
  52.                             PhotoCollection.ReloadData ();
  53.                             Console.WriteLine("number of photos now "+ photoSource.photos.Count);
  54.                         });
  55.                     }
  56.                 }
  57.             }
  58.  
  59.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement