public UIImage RequestImage(TKey id, string url, IUrlImageUpdated <TKey> notify) { //First see if the image is in memory cache already and return it if so lock (cache) { if (cache.ContainsKey(id)) { //Console.WriteLine("Loading from Memory Cache: " + id); return(cache[id]); } } //Next check for a saved file, and load it into cache and return it if found string picFile = picDir + "/" + id + ".png"; if (File.Exists(picFile)) { UIImage img = null; //Console.WriteLine("Loading from Cache: " + picFile); try { img = UIImage.FromFileUncached(picFile); } catch { } if (img != null) { AddToCache(id, img); //Add it to cache return(img); //Return this image } } //At this point the file needs to be downloaded, so queue it up to download //lock (queue) // { // queue.Enqueue(new UrlImageStoreRequest<TKey>() { Id = id, Url = url, Notify = notify }); // } //If we haven't maxed out our threads we should start another to download the images // if (threadCount < maxWorkers) // ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadImagesWorker)); //opQueue.AddOperation(new DownloadImageOperation<TKey>(this, id, url, notify)); opQueue.AddOperation(delegate { var img = UIImage.LoadFromData(NSData.FromUrl(NSUrl.FromString(url))); if (this.ProcessImage != null) { img = this.ProcessImage(img, id); } this.AddToCache(id, img); notify.UrlImageUpdated(id); }); //Return the default while they wait for the queued download return(this.DefaultImage); }
public Drawable RequestImage(string id, string url, IUrlImageUpdated notify) { if (string.IsNullOrEmpty(url)) // do not start for empty string, this can happen as cogenta might not have the actual string for the product/offer { return(null); } //Next check for a saved file, and load it into cache and return it if found string picFile = picDir + GenerateMD5(id) + ".png"; if (File.Exists(picFile)) { Drawable img = null; try { img = Drawable.CreateFromPath(picFile); } catch { } if (img != null) { return(img); //Return this image } } factory.StartNew(delegate { try { Drawable img = null; // var data = NSData.FromUrl (NSUrl.FromString(url)); // // if (data == null) // { // Console.WriteLine ("UrlImageStore: No data for URL: " + url); // return; // } // // var img = UIImage.LoadFromData(data); var req = System.Net.HttpWebRequest.Create(url) as System.Net.HttpWebRequest; using (var stream = req.GetResponse().GetResponseStream()) { img = Drawable.CreateFromStream(stream, ""); } img = this.ProcessImage(id, img); this.AddToCache(id, img); notify.UrlImageUpdated(id, img); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }); // Return the default while they wait for the queued download return(null); }
public UIImage RequestImage(string id, string url, IUrlImageUpdated notify) { if (string.IsNullOrEmpty(url)) // do not start for empty string, this can happen as cogenta might not have the actual string for the product/offer { return(null); } //Next check for a saved file, and load it into cache and return it if found string picFile = picDir + GenerateMD5(id) + ".png"; if (File.Exists(picFile)) { UIImage img = null; try { img = UIImage.FromFile(picFile); } catch { } if (img != null) { return(img); //Return this image } } factory.StartNew(delegate { try { var data = NSData.FromUrl(NSUrl.FromString(url)); if (data == null) { Console.WriteLine("UrlImageStore: No data for URL: " + url); return; } var img = UIImage.LoadFromData(data); img = this.ProcessImage(id, img); this.AddToCache(id, img); notify.UrlImageUpdated(id, img); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }); // Return the default while they wait for the queued download return(null); }
public UIImage RequestImage(string id, string url, IUrlImageUpdated notify) { if (string.IsNullOrEmpty(url)) // do not start for empty string, this can happen as cogenta might not have the actual string for the product/offer { return(null); } //Next check for a saved file, and load it into cache and return it if found string picFile = String.Format("{0}{1}.png", picDir, GenerateMD5(id)); bool shouldReturn; UIImage result = RequestImageImpl(picFile, out shouldReturn); if (shouldReturn) { return(result); } factory.StartNew(() => { try { var data = NSData.FromUrl(NSUrl.FromString(url)); if (data == null) { Console.WriteLine("UrlImageStore: No data for URL: " + url); return; } var img = UIImage.LoadFromData(data); img = ProcessImage(id, img); AddToCache(id, img); notify.UrlImageUpdated(id, img); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }); // Return the default while they wait for the queued download return(null); }
public UIImage RequestImage(string id, string url, IUrlImageUpdated notify) { if (string.IsNullOrEmpty (url)) // do not start for empty string, this can happen as cogenta might not have the actual string for the product/offer return null; //Next check for a saved file, and load it into cache and return it if found string picFile = picDir + GenerateMD5 (id) + ".png"; if (File.Exists(picFile)) { UIImage img = null; try { img = UIImage.FromFileUncached(picFile); } catch { } if (img != null) { return img; //Return this image } } factory.StartNew (delegate { try { var data = NSData.FromUrl (NSUrl.FromString(url)); if (data == null) { Console.WriteLine ("UrlImageStore: No data for URL: " + url); return; } var img = UIImage.LoadFromData(data); img = this.ProcessImage(id, img); this.AddToCache(id, img); notify.UrlImageUpdated(id, img); } catch (Exception ex) { Console.WriteLine (ex.ToString ()); } }); // Return the default while they wait for the queued download return null; }
/// <summary> /// Requests the image. /// </summary> /// <returns>The image.</returns> /// <param name="id">Identifier.</param> /// <param name="url">URL.</param> /// <param name="notify">Notify.</param> public UIImage RequestImage(string id, string url, IUrlImageUpdated notify) { if (string.IsNullOrEmpty (url)) return null; // do not start for empty string // next check for a saved file, and load it into cache and return it if found string cachedFilename = cachedImageDir + "/" + StringUtils.GenerateMD5 (id) + ".png"; if (File.Exists (cachedFilename)) { UIImage cachedImage = null; try { cachedImage = UIImage.FromFile (cachedFilename); } catch (Exception ex) { Console.WriteLine ("UrlImageStore: Error loading image from {0} ({1})\n{2}", cachedFilename, ex.Message, ex.StackTrace); } if (cachedImage != null) { return cachedImage; } } // download the image now DownloadImage (id, url, notify); // return the default while they wait for the queued download return null; }
/// <summary> /// Downloads the image. /// </summary> /// <param name="id">Identifier.</param> /// <param name="url">URL.</param> /// <param name="notify">Notify.</param> public void DownloadImage(string id, string url, IUrlImageUpdated notify) { taskFactory.StartNew (delegate { try { var data = NSData.FromUrl (NSUrl.FromString (url)); if (data == null) { Console.WriteLine ("UrlImageStore: No data for " + url); return; } var img = UIImage.LoadFromData (data); img = ProcessImage (id, img); AddToCache (id, img); notify.UrlImageUpdated (id, img); } catch (Exception ex) { Console.WriteLine ("UrlImageStore: Errorloading image {0} ({1})\n{2}", url, ex.Message, ex.StackTrace); } }); }
public UIImage RequestImage(string id, string url, IUrlImageUpdated notify) { if (string.IsNullOrEmpty (url)) // do not start for empty string, this can happen as cogenta might not have the actual string for the product/offer return null; //Next check for a saved file, and load it into cache and return it if found string picFile = String.Format("{0}{1}.png", picDir, GenerateMD5(id)); bool shouldReturn; UIImage result = RequestImageImpl(picFile, out shouldReturn); if (shouldReturn) return result; factory.StartNew(() => { try { var data = NSData.FromUrl(NSUrl.FromString(url)); if (data == null) { Console.WriteLine("UrlImageStore: No data for URL: " + url); return; } var img = UIImage.LoadFromData(data); img = ProcessImage(id, img); AddToCache(id, img); notify.UrlImageUpdated(id, img); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }); // Return the default while they wait for the queued download return null; }