ReaderThumbFetch.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // ReaderThumbFetch.m
  3. // Reader v2.9.0
  4. //
  5. // Created by Julius Oklamcak on 2011-09-01.
  6. // Copyright © 2011-2015 Julius Oklamcak. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights to
  11. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  12. // of the Software, and to permit persons to whom the Software is furnished to
  13. // do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. #import "ReaderThumbFetch.h"
  26. #import "ReaderThumbRender.h"
  27. #import "ReaderThumbCache.h"
  28. #import "ReaderThumbView.h"
  29. #import <ImageIO/ImageIO.h>
  30. @implementation ReaderThumbFetch
  31. {
  32. ReaderThumbRequest *request;
  33. }
  34. #pragma mark - ReaderThumbFetch instance methods
  35. - (instancetype)initWithRequest:(ReaderThumbRequest *)options
  36. {
  37. if ((self = [super initWithGUID:options.guid]))
  38. {
  39. request = options;
  40. }
  41. return self;
  42. }
  43. - (void)cancel
  44. {
  45. [super cancel]; // Cancel the operation
  46. request.thumbView.operation = nil; // Break retain loop
  47. request.thumbView = nil; // Release target thumb view on cancel
  48. [[ReaderThumbCache sharedInstance] removeNullForKey:request.cacheKey];
  49. }
  50. - (NSURL *)thumbFileURL
  51. {
  52. NSString *cachePath = [ReaderThumbCache thumbCachePathForGUID:request.guid]; // Thumb cache path
  53. NSString *fileName = [[NSString alloc] initWithFormat:@"%@.png", request.thumbName]; // Thumb file name
  54. return [NSURL fileURLWithPath:[cachePath stringByAppendingPathComponent:fileName]]; // File URL
  55. }
  56. - (void)main
  57. {
  58. CGImageRef imageRef = NULL; NSURL *thumbURL = [self thumbFileURL];
  59. CGImageSourceRef loadRef = CGImageSourceCreateWithURL((__bridge CFURLRef)thumbURL, NULL);
  60. if (loadRef != NULL) // Load the existing thumb image
  61. {
  62. imageRef = CGImageSourceCreateImageAtIndex(loadRef, 0, NULL); // Load it
  63. CFRelease(loadRef); // Release CGImageSource reference
  64. }
  65. else // Existing thumb image not found - so create and queue up a thumb render operation on the work queue
  66. {
  67. ReaderThumbRender *thumbRender = [[ReaderThumbRender alloc] initWithRequest:request]; // Create a thumb render operation
  68. [thumbRender setQueuePriority:self.queuePriority]; //[thumbRender setThreadPriority:(self.threadPriority - 0.1)]; // Priority
  69. if (self.isCancelled == NO) // We're not cancelled - so update things and add the render operation to the work queue
  70. {
  71. request.thumbView.operation = thumbRender; // Update the thumb view operation property to the new operation
  72. [[ReaderThumbQueue sharedInstance] addWorkOperation:thumbRender]; return; // Queue the operation
  73. }
  74. }
  75. if (imageRef != NULL) // Create a UIImage from a CGImage and show it
  76. {
  77. UIImage *image = [UIImage imageWithCGImage:imageRef scale:request.scale orientation:UIImageOrientationUp];
  78. CGImageRelease(imageRef); // Release the CGImage reference from the above thumb load code
  79. UIGraphicsBeginImageContextWithOptions(image.size, YES, request.scale); // Graphics context
  80. [image drawAtPoint:CGPointZero]; // Decode and draw the image on this background thread
  81. UIImage *decoded = UIGraphicsGetImageFromCurrentImageContext(); // Newly decoded image
  82. UIGraphicsEndImageContext(); // Cleanup after the bitmap-based graphics drawing context
  83. [[ReaderThumbCache sharedInstance] setObject:decoded forKey:request.cacheKey]; // Cache it
  84. if (self.isCancelled == NO) // Show the image in the target thumb view on the main thread
  85. {
  86. ReaderThumbView *thumbView = request.thumbView; // Target thumb view for image show
  87. NSUInteger targetTag = request.targetTag; // Target reference tag for image show
  88. dispatch_async(dispatch_get_main_queue(), // Queue image show on main thread
  89. ^{
  90. if (thumbView.targetTag == targetTag) [thumbView showImage:decoded];
  91. });
  92. }
  93. }
  94. request.thumbView.operation = nil; // Break retain loop
  95. }
  96. @end