Last active
November 30, 2015 01:50
-
-
Save skyline75489/2b6323c98b5e4ffa6d40 to your computer and use it in GitHub Desktop.
FLAnimatedImage+YYCache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FLAnimatedImageView+YYCache.m | |
// Gif-Example | |
// | |
// Created by skyline on 15/11/21. | |
// Copyright © 2015年 skyline. All rights reserved. | |
// | |
#import "FLAnimatedImageView+YYCache.h" | |
#import "SDWebImageDownloader.h" | |
#import "SDWebImageManager.h" | |
#import "FLAnimatedImageView.h" | |
#import "FLAnimatedImage.h" | |
#import "YYCache.h" | |
#import <objc/runtime.h> | |
static YYCache *_cache = nil; | |
static NSOperationQueue * _decodeOperationQueue = nil; | |
__attribute__((constructor)) | |
static void initialize_Cache() { | |
_cache = [[YYCache alloc] initWithName:@"TEST"]; | |
} | |
__attribute__((constructor)) | |
static void initialize_DecodeQueue() { | |
_decodeOperationQueue = [[NSOperationQueue alloc] init]; | |
} | |
@implementation FLAnimatedImageView (YYCache) | |
@dynamic decodeOperation; | |
@dynamic downloadOperation; | |
- (void)yy_setImageWithURL:(NSURL *)url progress:(void (^)(NSInteger, NSInteger))progressBlock completed:(void (^)(NSData *))completionBlock { | |
[self yy_cancelCurrentImageRequest]; | |
if ([_cache containsObjectForKey:url.absoluteString]) { | |
NSBlockOperation *operation = [[NSBlockOperation alloc] init]; | |
__weak NSBlockOperation *weakOperation = operation; | |
__weak typeof(self) weakSelf = self; | |
[operation addExecutionBlock:^{ | |
__strong typeof(self) strongSelf = weakSelf; | |
if(![weakOperation isCancelled]){ | |
NSData * data = [_cache objectForKey:url.absoluteString]; | |
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:data]; | |
if (![weakOperation isCancelled]) { | |
[strongSelf performSelectorOnMainThread:@selector(setAnimatedImage:) withObject:image waitUntilDone:NO modes:@[NSDefaultRunLoopMode]]; | |
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ | |
if (completionBlock) { | |
completionBlock(data); | |
} | |
}]; | |
} | |
} | |
}]; | |
self.decodeOperation = operation; | |
[_decodeOperationQueue addOperation:operation]; | |
return; | |
} | |
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader]; | |
__weak typeof(self) weakSelf = self; | |
NSOperation *downloadOperation = [downloader downloadImageWithURL:url options:0 progress:progressBlock completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { | |
__strong typeof(self) strongSelf = weakSelf; | |
if (image && finished) { | |
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:data]; | |
strongSelf.animatedImage = image; | |
if (completionBlock) { | |
completionBlock(data); | |
} | |
[_cache setObject:data forKey:url.absoluteString]; | |
} else { | |
NSLog(@"Download Failed"); | |
} | |
}]; | |
self.downloadOperation = downloadOperation; | |
} | |
- (void)yy_cancelCurrentImageRequest { | |
if (self.downloadOperation) { | |
[self.downloadOperation cancel]; | |
} | |
if (self.decodeOperation) { | |
[self.decodeOperation cancel]; | |
} | |
self.downloadOperation = nil; | |
self.decodeOperation = nil; | |
} | |
- (void)setDownloadOperation:(NSOperation *)downloadOperation { | |
objc_setAssociatedObject(self, @selector(downloadOperation), downloadOperation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
- (NSOperation *)downloadOperation { | |
return objc_getAssociatedObject(self, @selector(downloadOperation)); | |
} | |
- (void)setDecodeOperation:(NSOperation *)decodeOperation { | |
objc_setAssociatedObject(self, @selector(decodeOperation), decodeOperation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
- (NSOperation *)decodeOperation { | |
return objc_getAssociatedObject(self, @selector(decodeOperation)); | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment