Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@import DFImageManager;
@interface CustomImageLoadingView
- (void)prepareForReuse;
- (void)setImageWithRequest:(nonnull DFImageRequest *)request;
@end
@implementation CustomImageLoadingView {
DFImageTask *_task;
}
- (void)dealloc {
[self cancelFetching];
}
- (void)prepareForReuse {
[self cancelFetching];
self.image = nil;
}
- (void)cancelFetching {
_task.completionHandler = nil; // Make sure that completion handler is not called for obsolete tasks
[_task cancel];
_task = nil;
}
- (void)setImageWithRequest:(DFImageRequest *)request {
[self cancelFetching];
// TODO: Add activity indicator
typeof(self) __weak weakSelf = self;
_task = [DFImageManager imageTaskForRequest:request completion:^(UIImage *__nullable image, NSError *__nullable error, DFImageResponse *__nullable response, DFImageTask *__nonnull imageTask){
// TODO: Remove activity indicator
weakSelf.image = image;
if (!response.isFastResponse) { // Not from cache
// TODO: Add animations if necessary
}
}];
[_task resume];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment