VLCThumbnailsCache.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. UIImage *thumbnail;
  68. VLCThumbnailsCache *cache = [VLCThumbnailsCache sharedThumbnailCache];
  69. if ([object isKindOfClass:[MLShow class]]) {
  70. thumbnail = [cache thumbnailForShow:(MLShow *)object];
  71. } else if ([object isKindOfClass:[MLShowEpisode class]]) {
  72. MLFile *anyFileFromEpisode = [(MLShowEpisode *)object files].anyObject;
  73. thumbnail = [cache thumbnailForMediaFile:anyFileFromEpisode];
  74. } else if ([object isKindOfClass:[MLLabel class]]) {
  75. thumbnail = [cache thumbnailForLabel:(MLLabel *)object];
  76. } else if ([object isKindOfClass:[MLAlbum class]]) {
  77. thumbnail = [cache thumbnailForAlbum:(MLAlbum *)object];
  78. } else if ([object isKindOfClass:[MLAlbumTrack class]]) {
  79. thumbnail = [cache thumbnailForAlbumTrack:(MLAlbumTrack *)object];
  80. } else {
  81. thumbnail = [cache thumbnailForMediaFile:(MLFile *)object];
  82. }
  83. return thumbnail;
  84. }
  85. + (UIImage *)thumbnailForManagedObject:(NSManagedObject *)object toFitRect:(CGRect)rect shouldReplaceCache:(BOOL)replaceCache
  86. {
  87. UIImage *rawThumbnail = [self thumbnailForManagedObject:object];
  88. CGSize rawSize = rawThumbnail.size;
  89. /* scaling is potentially expensive, so we should avoid re-doing it for the same size over and over again */
  90. if (rawSize.width <= rect.size.width && rawSize.height <= rect.size.height)
  91. return rawThumbnail;
  92. UIImage *scaledImage = [UIImage scaleImage:rawThumbnail toFitRect:rect];
  93. if (replaceCache)
  94. [[VLCThumbnailsCache sharedThumbnailCache] _setThumbnail:scaledImage forObjectId:object.objectID];
  95. return scaledImage;
  96. }
  97. - (void)_setThumbnail:(UIImage *)image forObjectId:(NSManagedObjectID *)objID
  98. {
  99. if (image)
  100. [_thumbnailCache setObject:image forKey:objID];
  101. }
  102. - (UIImage *)thumbnailForMediaFile:(MLFile *)mediaFile
  103. {
  104. if (mediaFile == nil || mediaFile.objectID == nil)
  105. return nil;
  106. NSManagedObjectID *objID = mediaFile.objectID;
  107. UIImage *displayedImage = [_thumbnailCache objectForKey:objID];
  108. if (displayedImage)
  109. return displayedImage;
  110. if (!displayedImage) {
  111. __block UIImage *computedImage = nil;
  112. void (^getThumbnailBlock)(void) = ^(){
  113. computedImage = mediaFile.computedThumbnail;
  114. };
  115. if ([NSThread isMainThread])
  116. getThumbnailBlock();
  117. else
  118. dispatch_sync(dispatch_get_main_queue(), getThumbnailBlock);
  119. displayedImage = computedImage;
  120. }
  121. if (displayedImage)
  122. [_thumbnailCache setObject:displayedImage forKey:objID];
  123. return displayedImage;
  124. }
  125. - (UIImage *)thumbnailForShow:(MLShow *)mediaShow
  126. {
  127. NSManagedObjectID *objID = mediaShow.objectID;
  128. UIImage *displayedImage;
  129. BOOL forceRefresh = NO;
  130. NSUInteger count = [mediaShow.episodes count];
  131. NSNumber *previousCount = [_thumbnailCacheMetadata objectForKey:objID];
  132. if (previousCount.unsignedIntegerValue != count)
  133. forceRefresh = YES;
  134. if (!forceRefresh) {
  135. displayedImage = [_thumbnailCache objectForKey:objID];
  136. if (displayedImage)
  137. return displayedImage;
  138. }
  139. NSUInteger fileNumber = count > 3 ? 3 : count;
  140. NSArray *episodes = [mediaShow.episodes allObjects];
  141. NSMutableArray *files = [[NSMutableArray alloc] init];
  142. for (NSUInteger x = 0; x < count; x++) {
  143. /* this is a multi-threaded app, so the episode object might be there already,
  144. * but without an assigned file, so we need to check for its existance (#13128) */
  145. if ([episodes[x] files].anyObject != nil)
  146. [files addObject:[episodes[x] files].anyObject];
  147. }
  148. displayedImage = [self clusterThumbFromFiles:files andNumber:fileNumber blur:NO];
  149. if (displayedImage) {
  150. [_thumbnailCache setObject:displayedImage forKey:objID];
  151. [_thumbnailCacheMetadata setObject:@(count) forKey:objID];
  152. }
  153. return displayedImage;
  154. }
  155. - (UIImage *)thumbnailForLabel:(MLLabel *)mediaLabel
  156. {
  157. NSManagedObjectID *objID = mediaLabel.objectID;
  158. UIImage *displayedImage;
  159. BOOL forceRefresh = NO;
  160. NSUInteger count = [mediaLabel.files count];
  161. NSNumber *previousCount = [_thumbnailCacheMetadata objectForKey:objID];
  162. if (previousCount.unsignedIntegerValue != count)
  163. forceRefresh = YES;
  164. if (!forceRefresh) {
  165. displayedImage = [_thumbnailCache objectForKey:objID];
  166. if (displayedImage)
  167. return displayedImage;
  168. }
  169. NSUInteger fileNumber = count > 3 ? 3 : count;
  170. NSArray *files = [mediaLabel.files allObjects];
  171. BOOL blur = NO;
  172. if (SYSTEM_RUNS_IOS7_OR_LATER)
  173. blur = YES;
  174. displayedImage = [self clusterThumbFromFiles:files andNumber:fileNumber blur:blur];
  175. if (displayedImage) {
  176. [_thumbnailCache setObject:displayedImage forKey:objID];
  177. [_thumbnailCacheMetadata setObject:@(count) forKey:objID];
  178. }
  179. return displayedImage;
  180. }
  181. - (UIImage *)thumbnailForAlbum:(MLAlbum *)album
  182. {
  183. __block MLAlbumTrack *track = nil;
  184. void (^getFileBlock)(void) = ^(){
  185. track = [album tracks].anyObject;
  186. };
  187. if ([NSThread isMainThread])
  188. getFileBlock();
  189. else
  190. dispatch_sync(dispatch_get_main_queue(), getFileBlock);
  191. return [self thumbnailForAlbumTrack:track];
  192. }
  193. - (UIImage *)thumbnailForAlbumTrack:(MLAlbumTrack *)albumTrack
  194. {
  195. __block MLFile *anyFileFromAnyTrack = nil;
  196. void (^getFileBlock)(void) = ^(){
  197. anyFileFromAnyTrack = [albumTrack files].anyObject;
  198. };
  199. if ([NSThread isMainThread])
  200. getFileBlock();
  201. else
  202. dispatch_sync(dispatch_get_main_queue(), getFileBlock);
  203. return [self thumbnailForMediaFile:anyFileFromAnyTrack];
  204. }
  205. - (UIImage *)clusterThumbFromFiles:(NSArray *)files andNumber:(NSUInteger)fileNumber blur:(BOOL)blurImage
  206. {
  207. UIImage *clusterThumb;
  208. CGSize imageSize;
  209. if (_currentDeviceIdiom == UIUserInterfaceIdiomPad) {
  210. if ([UIScreen mainScreen].scale==2.0)
  211. imageSize = CGSizeMake(682., 384.);
  212. else
  213. imageSize = CGSizeMake(341., 192.);
  214. } else if (_currentDeviceIdiom == UIUserInterfaceIdiomPhone) {
  215. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  216. if ([UIScreen mainScreen].scale==2.0)
  217. imageSize = CGSizeMake(480., 270.);
  218. else
  219. imageSize = CGSizeMake(720., 405.);
  220. } else {
  221. if ([UIScreen mainScreen].scale==2.0)
  222. imageSize = CGSizeMake(258., 145.);
  223. else
  224. imageSize = CGSizeMake(129., 73.);
  225. }
  226. } else {
  227. if (SYSTEM_RUNS_IOS82_OR_LATER) {
  228. if (WKInterfaceDevice.currentDevice != nil) {
  229. CGRect screenRect = WKInterfaceDevice.currentDevice.screenBounds;
  230. imageSize = CGSizeMake(screenRect.size.width * WKInterfaceDevice.currentDevice.screenScale, 120.);
  231. }
  232. }
  233. }
  234. UIGraphicsBeginImageContext(imageSize);
  235. NSUInteger iter = files.count < fileNumber ? files.count : fileNumber;
  236. for (NSUInteger i = 0; i < iter; i++) {
  237. MLFile *file = [files objectAtIndex:i];
  238. clusterThumb = [self thumbnailForMediaFile:file];
  239. CGContextRef context = UIGraphicsGetCurrentContext();
  240. CGFloat imagePartWidth = (imageSize.width / iter);
  241. //the rect in which the image should be drawn
  242. CGRect clippingRect = CGRectMake(imagePartWidth * i, 0, imagePartWidth, imageSize.height);
  243. CGContextSaveGState(context);
  244. CGContextClipToRect(context, clippingRect);
  245. //take the center of the clippingRect and calculate the offset from the original center
  246. CGFloat centerOffset = (imagePartWidth * i + imagePartWidth / 2) - imageSize.width / 2;
  247. //shift the rect to draw the middle of the image in the clippingrect
  248. CGRect drawingRect = CGRectMake(centerOffset, 0, imageSize.width, imageSize.height);
  249. [clusterThumb drawInRect:drawingRect];
  250. //get rid of the old clippingRect
  251. CGContextRestoreGState(context);
  252. }
  253. clusterThumb = UIGraphicsGetImageFromCurrentImageContext();
  254. UIGraphicsEndImageContext();
  255. if (!blurImage)
  256. return clusterThumb;
  257. return [UIImage applyBlurOnImage:clusterThumb withRadius:0.1];
  258. }
  259. @end