Skip to content
Fabien Molinet edited this page Dec 17, 2015 · 12 revisions

Source

Easily load any image from any source.

Internet

Or from an URL. In this case the image is cached (by default 30 days but there is an optional TimeSpan so you can choose yours).

ImageService.LoadUrl(urlToImage).Into(_imageView);

File

when you want to load the image from a file or application bundle:

ImageService.LoadFile(fullPathToImage).Into(_imageView);

Remark: on iOS both ImageService.LoadFileFromApplicationBundle and ImageService.CompiledResource have the same effect as ImageService.LoadFile.

Callbacks

In order to be notified when the image is successfully loaded or when there were errors:

Success

ImageService.LoadUrl(urlToImage)
.Success(() =>
{
  // your code here...
})

An overload exists to get dimensions and image source: Success((size, loadingResult) => {}) The image source, of type LoadingResult, indicates if it was loaded from web, disk cache, memory cache, file, bundle, ...

Error

.Error(exception =>
{
  // your code here...
})
.Into(_imageView);

Process finished

Finish callback is invoked when image loading process finished, whatever the result, success, failure or cancellation:

ImageService.LoadUrl(urlToImage)
.Finish(workScheduled =>
{
  // your code here...
})
.Into(_imageView);

Argument workScheduled, of type IScheduledWork, indicates if it succeeded, failed or was cancelled.

Placeholders

It is possible to define placeholders while image is loading or when an error occured.

Loading

ImageService.LoadUrl(urlToImage)
.LoadingPlaceholder("loading.png") // placeholder loaded from file
.Into(_imageView);

By default placeholders are loaded from file but you can also specify the ImageSource (see below).

Error

ImageService.LoadUrl(urlToImage)
.ErrorPlaceholder("http://mydomain.com/error.png", ImageSource.Url) // placeholder loaded from a URL
.Into(_imageView);

Retry

If your download failed, or something wrong happened, it can be automatically retried.

In this example, if loading from the URL failed then it will try 3 more times with a 200ms interval between each trial:

ImageService.LoadUrl(urlToImage)
.Retry(3, 200)
.Into(_imageView);

Limit memory usage

Reduce image size

Take advantage of DownSample to reduce memory usage from images. You can redefine width/height to the visible size. Users won't notice the difference but their phone memory will! Even though method allows two optional parameters you should only provide either width or height. Aspect ratio will be kept based on provided value.

// In this example height will be calculated to match width while keeping the same aspect ratio.
ImageService.LoadUrl(urlToImage).DownSample(width: 150).Into(_imageView);

Transformations

Transformations allow a whole new range of possibilities. Crop an image on the fly, change its colors, make it rounded... They are explained here.

Example:

// Image will have gray colors only.
ImageService.LoadUrl(urlToImage)
.Transform(new GrayscaleTransformation())
.Into(imgDisplay);