VLCThumbnailsCache.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*****************************************************************************
  2. * VLCThumbnailsCache.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Gleb Pinigin <gpinigin # gmail.com>
  9. * Felix Paul Kühne <fkuehne # videolan.org>
  10. * Carola Nitz <caro # videolan.org>
  11. * Tobias Conradi <videolan # tobias-conradi.de>
  12. *
  13. * Refer to the COPYING file of the official project for license.
  14. *****************************************************************************/
  15. #import "VLC for iOS-Prefix.pch"
  16. #import "VLCThumbnailsCache.h"
  17. #import <CommonCrypto/CommonDigest.h>
  18. #import "UIImage+Blur.h"
  19. #import "UIImage+Scaling.h"
  20. #import <WatchKit/WatchKit.h>
  21. @interface VLCThumbnailsCache() {
  22. NSInteger MaxCacheSize;
  23. NSCache *_thumbnailCache;
  24. NSCache *_thumbnailCacheMetadata;
  25. NSInteger _currentDeviceIdiom;
  26. }
  27. @end
  28. @implementation VLCThumbnailsCache
  29. #define MAX_CACHE_SIZE_IPHONE 21 // three times the number of items shown on iPhone 5
  30. #define MAX_CACHE_SIZE_IPAD 27 // three times the number of items shown on iPad
  31. #define MAX_CACHE_SIZE_WATCH 15 // three times the number of items shown on 42mm Watch
  32. - (instancetype)init
  33. {
  34. self = [super init];
  35. if (self) {
  36. _currentDeviceIdiom = [[UIDevice currentDevice] userInterfaceIdiom];
  37. MaxCacheSize = 0;
  38. switch (_currentDeviceIdiom) {
  39. case UIUserInterfaceIdiomPad:
  40. MaxCacheSize = MAX_CACHE_SIZE_IPAD;
  41. break;
  42. case UIUserInterfaceIdiomPhone:
  43. MaxCacheSize = MAX_CACHE_SIZE_IPHONE;
  44. break;
  45. default:
  46. MaxCacheSize = MAX_CACHE_SIZE_WATCH;
  47. break;
  48. }
  49. _thumbnailCache = [[NSCache alloc] init];
  50. _thumbnailCacheMetadata = [[NSCache alloc] init];
  51. [_thumbnailCache setCountLimit: MaxCacheSize];
  52. [_thumbnailCacheMetadata setCountLimit: MaxCacheSize];
  53. }
  54. return self;
  55. }
  56. + (instancetype)sharedThumbnailCache
  57. {
  58. static dispatch_once_t onceToken;
  59. static VLCThumbnailsCache *sharedThumbnailCache;
  60. dispatch_once(&onceToken, ^{
  61. sharedThumbnailCache = [[VLCThumbnailsCache alloc] init];
  62. });
  63. return sharedThumbnailCache;
  64. }
  65. + (UIImage *)thumbnailForManagedObject:(NSManagedObject *)object
  66. {
  67. return [self thumbnailForManagedObject:object refreshCache:NO];
  68. }
  69. + (UIImage *)thumbnailForManagedObject:(NSManagedObject *)object
  70. refreshCache:(BOOL)refreshCache
  71. {
  72. UIImage *thumbnail;
  73. VLCThumbnailsCache *cache = [VLCThumbnailsCache sharedThumbnailCache];
  74. if ([object isKindOfClass:[MLShow class]]) {
  75. thumbnail = [cache thumbnailForShow:(MLShow *)object refreshCache:refreshCache];
  76. } else if ([object isKindOfClass:[MLShowEpisode class]]) {
  77. MLFile *anyFileFromEpisode = [(MLShowEpisode *)object files].anyObject;
  78. thumbnail = [cache thumbnailForMediaFile:anyFileFromEpisode refreshCache:refreshCache];
  79. } else if ([object isKindOfClass:[MLLabel class]]) {
  80. thumbnail = [cache thumbnailForLabel:(MLLabel *)object refreshCache:refreshCache];
  81. } else if ([object isKindOfClass:[MLAlbum class]]) {
  82. thumbnail = [cache thumbnailForAlbum:(MLAlbum *)object refreshCache:refreshCache];
  83. } else if ([object isKindOfClass:[MLAlbumTrack class]]) {
  84. thumbnail = [cache thumbnailForAlbumTrack:(MLAlbumTrack *)object refreshCache:refreshCache];
  85. } else {
  86. thumbnail = [cache thumbnailForMediaFile:(MLFile *)object refreshCache:refreshCache];
  87. }
  88. return thumbnail;
  89. }
  90. + (UIImage *)thumbnailForManagedObject:(NSManagedObject *)object toFitRect:(CGRect)rect shouldReplaceCache:(BOOL)replaceCache
  91. {
  92. UIImage *rawThumbnail = [self thumbnailForManagedObject:object];
  93. CGSize rawSize = rawThumbnail.size;
  94. /* scaling is potentially expensive, so we should avoid re-doing it for the same size over and over again */
  95. if (rawSize.width <= rect.size.width && rawSize.height <= rect.size.height)
  96. return rawThumbnail;
  97. UIImage *scaledImage = [UIImage scaleImage:rawThumbnail toFitRect:rect];
  98. if (replaceCache)
  99. [[VLCThumbnailsCache sharedThumbnailCache] _setThumbnail:scaledImage forObjectId:object.objectID];
  100. return scaledImage;
  101. }
  102. - (void)_setThumbnail:(UIImage *)image forObjectId:(NSManagedObjectID *)objID
  103. {
  104. if (image)
  105. [_thumbnailCache setObject:image forKey:objID];
  106. }
  107. - (UIImage *)thumbnailForMediaFile:(MLFile *)mediaFile refreshCache:(BOOL)refreshCache
  108. {
  109. if (mediaFile == nil || mediaFile.objectID == nil)
  110. return nil;
  111. NSManagedObjectID *objID = mediaFile.objectID;
  112. UIImage *displayedImage;
  113. if (!refreshCache) {
  114. displayedImage = [_thumbnailCache objectForKey:objID];
  115. if (displayedImage)
  116. return displayedImage;
  117. }
  118. if (!displayedImage) {
  119. __block UIImage *computedImage = nil;
  120. void (^getThumbnailBlock)(void) = ^(){
  121. computedImage = mediaFile.computedThumbnail;
  122. };
  123. if ([NSThread isMainThread])
  124. getThumbnailBlock();
  125. else
  126. dispatch_sync(dispatch_get_main_queue(), getThumbnailBlock);
  127. displayedImage = computedImage;
  128. }
  129. if (displayedImage)
  130. [_thumbnailCache setObject:displayedImage forKey:objID];
  131. return displayedImage;
  132. }
  133. - (UIImage *)thumbnailForShow:(MLShow *)mediaShow refreshCache:(BOOL)refreshCache
  134. {
  135. NSManagedObjectID *objID = mediaShow.objectID;
  136. UIImage *displayedImage;
  137. BOOL forceRefresh = NO;
  138. NSUInteger count = [mediaShow.episodes count];
  139. NSNumber *previousCount = [_thumbnailCacheMetadata objectForKey:objID];
  140. if (previousCount.unsignedIntegerValue != count)
  141. forceRefresh = YES;
  142. if (refreshCache)
  143. forceRefresh = YES;
  144. if (!forceRefresh) {
  145. displayedImage = [_thumbnailCache objectForKey:objID];
  146. if (displayedImage)
  147. return displayedImage;
  148. }
  149. NSUInteger fileNumber = count > 3 ? 3 : count;
  150. NSArray *episodes = [mediaShow.episodes allObjects];
  151. NSMutableArray *files = [[NSMutableArray alloc] init];
  152. for (NSUInteger x = 0; x < count; x++) {
  153. /* this is a multi-threaded app, so the episode object might be there already,
  154. * but without an assigned file, so we need to check for its existance (#13128) */
  155. if ([episodes[x] files].anyObject != nil)
  156. [files addObject:[episodes[x] files].anyObject];
  157. }
  158. displayedImage = [self clusterThumbFromFiles:files andNumber:fileNumber blur:NO];
  159. if (displayedImage) {
  160. [_thumbnailCache setObject:displayedImage forKey:objID];
  161. [_thumbnailCacheMetadata setObject:@(count) forKey:objID];
  162. }
  163. return displayedImage;
  164. }
  165. - (UIImage *)thumbnailForLabel:(MLLabel *)mediaLabel refreshCache:(BOOL)refreshCache
  166. {
  167. NSManagedObjectID *objID = mediaLabel.objectID;
  168. UIImage *displayedImage;
  169. BOOL forceRefresh = NO;
  170. NSUInteger count = [mediaLabel.files count];
  171. NSNumber *previousCount = [_thumbnailCacheMetadata objectForKey:objID];
  172. if (previousCount.unsignedIntegerValue != count)
  173. forceRefresh = YES;
  174. if (refreshCache)
  175. forceRefresh = YES;
  176. if (!forceRefresh) {
  177. displayedImage = [_thumbnailCache objectForKey:objID];
  178. if (displayedImage)
  179. return displayedImage;
  180. }
  181. NSUInteger fileNumber = count > 3 ? 3 : count;
  182. NSArray *files = [mediaLabel.files allObjects];
  183. displayedImage = [self clusterThumbFromFiles:files andNumber:fileNumber blur:YES];
  184. if (displayedImage) {
  185. [_thumbnailCache setObject:displayedImage forKey:objID];
  186. [_thumbnailCacheMetadata setObject:@(count) forKey:objID];
  187. }
  188. return displayedImage;
  189. }
  190. - (UIImage *)thumbnailForAlbum:(MLAlbum *)album refreshCache:(BOOL)refreshCache
  191. {
  192. __block MLAlbumTrack *track = nil;
  193. void (^getFileBlock)(void) = ^(){
  194. track = [album tracks].anyObject;
  195. };
  196. if ([NSThread isMainThread])
  197. getFileBlock();
  198. else
  199. dispatch_sync(dispatch_get_main_queue(), getFileBlock);
  200. return [self thumbnailForAlbumTrack:track refreshCache:refreshCache];
  201. }
  202. - (UIImage *)thumbnailForAlbumTrack:(MLAlbumTrack *)albumTrack refreshCache:(BOOL)refreshCache
  203. {
  204. __block MLFile *anyFileFromAnyTrack = nil;
  205. void (^getFileBlock)(void) = ^(){
  206. anyFileFromAnyTrack = [albumTrack files].anyObject;
  207. };
  208. if ([NSThread isMainThread])
  209. getFileBlock();
  210. else
  211. dispatch_sync(dispatch_get_main_queue(), getFileBlock);
  212. return [self thumbnailForMediaFile:anyFileFromAnyTrack refreshCache:refreshCache];
  213. }
  214. - (UIImage *)clusterThumbFromFiles:(NSArray *)files andNumber:(NSUInteger)fileNumber blur:(BOOL)blurImage
  215. {
  216. UIImage *clusterThumb;
  217. CGSize imageSize;
  218. if (_currentDeviceIdiom == UIUserInterfaceIdiomPad) {
  219. if ([UIScreen mainScreen].scale==2.0)
  220. imageSize = CGSizeMake(682., 384.);
  221. else
  222. imageSize = CGSizeMake(341., 192.);
  223. } else if (_currentDeviceIdiom == UIUserInterfaceIdiomPhone) {
  224. if ([UIScreen mainScreen].scale==2.0)
  225. imageSize = CGSizeMake(480., 270.);
  226. else
  227. imageSize = CGSizeMake(720., 405.);
  228. } else {
  229. if (SYSTEM_RUNS_IOS82_OR_LATER) {
  230. if (WKInterfaceDevice.currentDevice != nil) {
  231. CGRect screenRect = WKInterfaceDevice.currentDevice.screenBounds;
  232. imageSize = CGSizeMake(screenRect.size.width * WKInterfaceDevice.currentDevice.screenScale, 120.);
  233. }
  234. }
  235. }
  236. UIGraphicsBeginImageContext(imageSize);
  237. NSUInteger iter = files.count < fileNumber ? files.count : fileNumber;
  238. for (NSUInteger i = 0; i < iter; i++) {
  239. MLFile *file = [files objectAtIndex:i];
  240. clusterThumb = [self thumbnailForMediaFile:file refreshCache:NO];
  241. CGContextRef context = UIGraphicsGetCurrentContext();
  242. CGFloat imagePartWidth = (imageSize.width / iter);
  243. //the rect in which the image should be drawn
  244. CGRect clippingRect = CGRectMake(imagePartWidth * i, 0, imagePartWidth, imageSize.height);
  245. CGContextSaveGState(context);
  246. CGContextClipToRect(context, clippingRect);
  247. //take the center of the clippingRect and calculate the offset from the original center
  248. CGFloat centerOffset = (imagePartWidth * i + imagePartWidth / 2) - imageSize.width / 2;
  249. //shift the rect to draw the middle of the image in the clippingrect
  250. CGRect drawingRect = CGRectMake(centerOffset, 0, imageSize.width, imageSize.height);
  251. [clusterThumb drawInRect:drawingRect];
  252. //get rid of the old clippingRect
  253. CGContextRestoreGState(context);
  254. }
  255. clusterThumb = UIGraphicsGetImageFromCurrentImageContext();
  256. UIGraphicsEndImageContext();
  257. if (!blurImage)
  258. return clusterThumb;
  259. return [UIImage applyBlurOnImage:clusterThumb withRadius:0.1];
  260. }
  261. @end