123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #import "ReaderThumbRender.h"
- #import "ReaderThumbCache.h"
- #import "ReaderThumbView.h"
- #import "CGPDFDocument.h"
- #import <ImageIO/ImageIO.h>
- @implementation ReaderThumbRender
- {
- ReaderThumbRequest *request;
- }
- #pragma mark - ReaderThumbRender 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
- {
- NSFileManager *fileManager = [NSFileManager new];
- NSString *cachePath = [ReaderThumbCache thumbCachePathForGUID:request.guid];
- [fileManager createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:NULL];
- NSString *fileName = [[NSString alloc] initWithFormat:@"%@.png", request.thumbName];
- return [NSURL fileURLWithPath:[cachePath stringByAppendingPathComponent:fileName]];
- }
- - (void)main
- {
- NSInteger page = request.thumbPage; NSString *password = request.password;
- CGImageRef imageRef = NULL; CFURLRef fileURL = (__bridge CFURLRef)request.fileURL;
- CGPDFDocumentRef thePDFDocRef = CGPDFDocumentCreateUsingUrl(fileURL, password);
- if (thePDFDocRef != NULL)
- {
- CGPDFPageRef thePDFPageRef = CGPDFDocumentGetPage(thePDFDocRef, page);
- if (thePDFPageRef != NULL)
- {
- CGFloat thumb_w = request.thumbSize.width;
- CGFloat thumb_h = request.thumbSize.height;
- CGRect cropBoxRect = CGPDFPageGetBoxRect(thePDFPageRef, kCGPDFCropBox);
- CGRect mediaBoxRect = CGPDFPageGetBoxRect(thePDFPageRef, kCGPDFMediaBox);
- CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect);
- NSInteger pageRotate = CGPDFPageGetRotationAngle(thePDFPageRef);
- CGFloat page_w = 0.0f; CGFloat page_h = 0.0f;
- switch (pageRotate)
- {
- default:
- case 0: case 180:
- {
- page_w = effectiveRect.size.width;
- page_h = effectiveRect.size.height;
- break;
- }
- case 90: case 270:
- {
- page_h = effectiveRect.size.width;
- page_w = effectiveRect.size.height;
- break;
- }
- }
- CGFloat scale_w = (thumb_w / page_w);
- CGFloat scale_h = (thumb_h / page_h);
- CGFloat scale = 0.0f;
- if (page_h > page_w)
- scale = ((thumb_h > thumb_w) ? scale_w : scale_h);
- else
- scale = ((thumb_h < thumb_w) ? scale_h : scale_w);
- NSInteger target_w = (page_w * scale);
- NSInteger target_h = (page_h * scale);
- if (target_w % 2) target_w--; if (target_h % 2) target_h--;
- target_w *= request.scale; target_h *= request.scale;
- CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
- CGBitmapInfo bmi = (kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst);
- CGContextRef context = CGBitmapContextCreate(NULL, target_w, target_h, 8, 0, rgb, bmi);
- if (context != NULL)
- {
- CGRect thumbRect = CGRectMake(0.0f, 0.0f, target_w, target_h);
- CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); CGContextFillRect(context, thumbRect);
- CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(thePDFPageRef, kCGPDFCropBox, thumbRect, 0, true));
-
- CGContextDrawPDFPage(context, thePDFPageRef);
- imageRef = CGBitmapContextCreateImage(context);
- CGContextRelease(context);
- }
- CGColorSpaceRelease(rgb);
- }
- CGPDFDocumentRelease(thePDFDocRef);
- }
- if (imageRef != NULL)
- {
- UIImage *image = [UIImage imageWithCGImage:imageRef scale:request.scale orientation:UIImageOrientationUp];
- [[ReaderThumbCache sharedInstance] setObject:image 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:image];
- });
- }
- CFURLRef thumbURL = (__bridge CFURLRef)[self thumbFileURL];
- CGImageDestinationRef thumbRef = CGImageDestinationCreateWithURL(thumbURL, (CFStringRef)@"public.png", 1, NULL);
- if (thumbRef != NULL)
- {
- CGImageDestinationAddImage(thumbRef, imageRef, NULL);
- CGImageDestinationFinalize(thumbRef);
- CFRelease(thumbRef);
- }
- CGImageRelease(imageRef);
- }
- else
- {
- [[ReaderThumbCache sharedInstance] removeNullForKey:request.cacheKey];
- }
- request.thumbView.operation = nil;
- }
- @end
|