Replacement for UIImageView that adds asynchronous load capability, caching, activity indicator, load progress bar & default image.
LTAsyncImageView is a powerful replacement for UIImageView.
What it adds:
We use it in several applications, inside tableViews for example, to load images illustrating lists.
To install the component into your project, simply Drag'n Drop both LTAsyncImageView.h & LTAsyncImageView.m into your project :
First, ensure that LTAsyncImageView.h is included into the header file of your view controller, or into the yourproject-prefix.pch if you want it to be accessible from any view / viewController :
If you want to transform an UIImageView from your xib file into LTAsyncImageView, simply change its type from UIImageView to LTAsyncImageView (as it is a descendant of UIImaveView) :
Then, don't forget to link your image from the nib editor into the header definitions :
If you use LTAsyncImageView as a component from the nib editor, then the image you'll set on it will automatically be considered as the default image.
Otherwise, you can manually set it :
[myImage setDefaultImage:[UIImage imageNamed:@"MyDefaultImage.png"]]; // where MyDefaultImage.png is the name of the image you want to be set as default.
If you want the component to load a distant image, you can use the following method:
This will load the distant image, and show the activity indicator and the progress-bar if it has been set to YES (default values).
LTAsyncImageView comes with an internal cache system that will prevent your servers from being charged by to many request of the same resource by the same user (for example if your imageview is inside an UITableView and the user scrolls up/down).
The default duration time of cache retention is set to 24 hours.
It can be changed by using the following methods :
- (void)setCacheDurationForDays:(int)nbDays; // set the cache duration in days - (void)setCacheDurationForHours:(int)nbHours; // set the cache duration in hours - (void)setCacheDurationForMinutes:(int)nbMinutes; // set the cache duration in minutes
To ask LTAsyncImageView to load an external image using the cache system, invoque this method :
When calling this method, the system will search if the image has not been previously loaded and if its "age" feet into the cache duration value. If so, then it is directly presented into LTAsyncImageView. If not, it will load it externally (showing activity indicator & progress if needed), then save it for a future reuse.
When LTAsyncImageView is loading an image from the internet, you can decide to show an UIActivityIndicator in front of the default image to let the user understand something is loading :
If you want to hide it, simply set its value to NO.
When LTAsyncImageView is loading an image from the internet, you can decide to show an ProgressBar in front of the default image to let the user understand something is loading, and to show him when the image will be available to show :
If you want to hide it, simply set its value to NO.
By default, the component respects the aspect-ratio, that means that if the loaded image aspect-ratio is not the same as the LTAsyncImageView width & height ratio, then if will not fill it entirely.
If you want to bypass this, and ensure the image will fill entirely the view, even if the resulted image is deformed, call this method:
As a bonus, LTAsyncImageView can shake itself the same way our component LTShakeAndDeleteView does.
To invoque the "shaking" of LTAsyncImageView, use :
And to stop it, simply use :
- (void)setDefaultImage:(UIImage *)image;
Sets the default image. The default image is an image shown when no other image has been loaded by the component.
- (void)startPreloadingDistantPictureWithUrl:(NSString *)url;
Ask the component to load a distant image, from a web server (it can be a jpeg or a png file). When calling it, the default image is shown, the ActivityIndicator and the ProgressBar too (if they've not been disabled), the distant file is loaded, then everything fade in/out for the final image to be shown.
- (void)startPreloadingAndCacheDistantPictureWithUrl:(NSString *)url;
Same main functionality as for startPreloadingDistantPictureWithUrl, it adds a cache feature, that prevent from loading again and again through the webserver if it has already been loaded.
Sets the cache duration (the duration telling not to reload an image again) in nbDays days.
Sets the cache duration (the duration telling not to reload an image again) in nbHours hours.
Sets the cache duration (the duration telling not to reload an image again) in nbMinutes minutes.
Start shaking the view, using the same effect as for our other component Shake And Delete View for iOS
Stop shaking the view.
If set to YES, then the aspect ratio will be kept to prevent image deformation.
If set to YES, then a download progressBar will be shown to see how much size has currently been loaded.
If set to Yes, then an activityIndicator will be shown while loading from the net.
- (void)LTAsyncImageReceived:(UIImage *)image forView:(id)view;
Called when a distant image has successfully been loaded. It returns the loaded image and a pointer to the LTAsyncImageView affected.
- (void)LTAsyncImageError:(NSString *)error;
Called when a network error occured when trying to load a distant image.