Skip to content

Instantly share code, notes, and snippets.

@cherifya
Last active December 27, 2016 07:10
Show Gist options
  • Save cherifya/5181759 to your computer and use it in GitHub Desktop.
Save cherifya/5181759 to your computer and use it in GitHub Desktop.
Improve SDWebImage library with support for a UIActivityIndicator and optional image transition. The transition animation is a simple fade-in.
//
// UIImageView+Webcache_Animation.m
// Tubesmix
//
// Created by Cherif YAYA on 17/03/13.
// Copyright (c) 2013 Cherif YAYA. All rights reserved.
//
#import "UIImageView+Webcache_Animation.h"
@implementation UIImageView (Webcache_Animation)
-(void) setImageWithURL:(NSURL *)url usingActivityIndicatorStyle : (UIActivityIndicatorViewStyle) activityStyle animated:(BOOL)animated {
[self setImageWithURL:url placeholderImage:nil usingActivityIndicatorStyle:activityStyle animated:animated];
}
-(void) setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage usingActivityIndicatorStyle : (UIActivityIndicatorViewStyle) activityStyle animated:(BOOL)animated {
__block UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:activityStyle];
activityIndicator.hidesWhenStopped = YES;
[self addSubview:activityIndicator];
activityIndicator.frame = self.bounds;
[activityIndicator startAnimating];
__block UIImageView *weakSelf = self;
[self setImageWithURL:url placeholderImage:placeholderImage
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
[activityIndicator removeFromSuperview];
//show animation if needed
if (image && cacheType == SDImageCacheTypeNone && animated)
{
weakSelf.alpha = 0.0;
[UIView animateWithDuration:0.35f
animations:^{
weakSelf.alpha = 1.0;
}];
}
}];
}
@end
#import "UIImageView+Webcache.h"
@interface UIImageView (Webcache_Animation)
/**
* Set the imageView `image` with an `url`.
* and put an activity indicator while downloading the.
*
* The downloand is asynchronous and cached. And the image display can be animated.
*
* @param url The url for the image.
* @param activityStyle THe UIActivityIndicatorViewStyle to use
* @param animated Whether the image display is animated or not
*/
-(void) setImageWithURL:(NSURL *)url usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle animated:(BOOL)animated;
/**
* Set the imageView `image` with an `url`.
* and put an activity indicator while downloading the.
*
* The download is asynchronous and cached. And the image display can be animated.
*
* @param url The url for the image.
* @param placeholderImage An optional image placeholder
* @param activityStyle THe UIActivityIndicatorViewStyle to use
* @param animated Whether the image display is animated or not
*/
-(void) setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle animated:(BOOL)animated;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment