Skip to content

Instantly share code, notes, and snippets.

@Redth
Created May 19, 2010 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Redth/405923 to your computer and use it in GitHub Desktop.
Save Redth/405923 to your computer and use it in GitHub Desktop.
using System;
using MonoTouch.UIKit;
using MonoTouch.UrlImageStore;
namespace FbGroups
{
public class UrlImageStoreExample : IUrlImageUpdated<string>
{
//Event to raise when our image is lazily loaded
public delegate void ImageLoadedDelegate(string id, UIImage image);
public event ImageLoadedDelegate ImageLoaded;
//Our UrlImageStore
UrlImageStore<string> imageStore;
public UrlImageStoreExample()
{
//This would be what you want to display while the image is lazily loaded
UIImage defaultImage = UIImage.FromFileUncached("DefaultUrlStoreImage.png");
//Create our image store with a capacity of 50 in its cache
// the image store's name is used to create a folder to save cached images to
// so keep this in mind and avoid collisions
//The processimagedelegate (see line 54) lets us do whatever we need to our images
// before they get cached
imageStore = new UrlImageStore<string>(50, "exampleStore", processImage);
imageStore.DefaultImage = defaultImage;
}
public UIImage RequestImage(string id, string url)
{
//We'll pass off the request to the actual image store, which will either return the
// actual image if it's already cached (either on disk or in memory)
// or it will queue up the lazy load, and we'll be notified (this) when that is done
return imageStore.RequestImage(id, url, this);
}
//This is from the IUrlImageUpdated interface, and lets us be told when an image has been loaded
// if it was lazily loaded.
// this never gets called if the initial ImageStore.RequestImage call finds the image in the cache
// since that would not be necessary
public void UrlImageUpdated (string id)
{
//Silly to in a way, re-raise this 'event', but just showing an example
// You'd likely want to use this method to set your UIImageView.Image = imageStore.GetImage(id)
// or something similar
if (this.ImageLoaded != null)
this.ImageLoaded(id, imageStore.GetImage(id));
}
//This handles our ProcessImageDelegate
// just a simple way for us to be able to do whatever we want to our image
// before it gets cached, so here's where you want to resize, etc.
UIImage processImage(UIImage image, string key)
{
//First scale it down to a max of 48 width OR 48 height,
// but keeping proporations
var result = Graphics.Scale(image, 48);
//Next, round the corners of the scaled image
result = Graphics.RoundCorners(result);
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment