123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- #import "ReaderThumbFetch.h"
- #import "ReaderThumbRender.h"
- #import "ReaderThumbCache.h"
- #import "ReaderThumbView.h"
- #import <ImageIO/ImageIO.h>
- @implementation ReaderThumbFetch
- {
- ReaderThumbRequest *request;
- }
- #pragma mark - ReaderThumbFetch instance methods
- - (instancetype)initWithRequest:(ReaderThumbRequest *)options
- {
- if ((self = [super initWithGUID:options.guid]))
- {
- request = options;
- }
- return self;
- }
- - (void)cancel
- {
- [super cancel];
- request.thumbView.operation = nil;
- request.thumbView = nil;
- [[ReaderThumbCache sharedInstance] removeNullForKey:request.cacheKey];
- }
- - (NSURL *)thumbFileURL
- {
- NSString *cachePath = [ReaderThumbCache thumbCachePathForGUID:request.guid];
- NSString *fileName = [[NSString alloc] initWithFormat:@"%@.png", request.thumbName];
- return [NSURL fileURLWithPath:[cachePath stringByAppendingPathComponent:fileName]];
- }
- - (void)main
- {
- CGImageRef imageRef = NULL; NSURL *thumbURL = [self thumbFileURL];
- CGImageSourceRef loadRef = CGImageSourceCreateWithURL((__bridge CFURLRef)thumbURL, NULL);
- if (loadRef != NULL)
- {
- imageRef = CGImageSourceCreateImageAtIndex(loadRef, 0, NULL);
- CFRelease(loadRef);
- }
- else
- {
- ReaderThumbRender *thumbRender = [[ReaderThumbRender alloc] initWithRequest:request];
- [thumbRender setQueuePriority:self.queuePriority];
- if (self.isCancelled == NO)
- {
- request.thumbView.operation = thumbRender;
- [[ReaderThumbQueue sharedInstance] addWorkOperation:thumbRender]; return;
- }
- }
- if (imageRef != NULL)
- {
- UIImage *image = [UIImage imageWithCGImage:imageRef scale:request.scale orientation:UIImageOrientationUp];
- CGImageRelease(imageRef);
- UIGraphicsBeginImageContextWithOptions(image.size, YES, request.scale);
- [image drawAtPoint:CGPointZero];
- UIImage *decoded = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- [[ReaderThumbCache sharedInstance] setObject:decoded forKey:request.cacheKey];
- if (self.isCancelled == NO)
- {
- ReaderThumbView *thumbView = request.thumbView;
- NSUInteger targetTag = request.targetTag;
- dispatch_async(dispatch_get_main_queue(),
- ^{
- if (thumbView.targetTag == targetTag) [thumbView showImage:decoded];
- });
- }
- }
- request.thumbView.operation = nil;
- }
- @end
|