VLCThumbnailsCache.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLC for iOS-Prefix.pch"
  15. #import "VLCThumbnailsCache.h"
  16. #import <CommonCrypto/CommonDigest.h>
  17. #import "UIImage+Blur.h"
  18. static NSInteger MaxCacheSize;
  19. static NSCache *_thumbnailCache;
  20. static NSCache *_thumbnailCacheMetadata;
  21. static NSInteger _currentDeviceIdiom;
  22. @implementation VLCThumbnailsCache
  23. #define MAX_CACHE_SIZE_IPHONE 21 // three times the number of items shown on iPhone 5
  24. #define MAX_CACHE_SIZE_IPAD 27 // three times the number of items shown on iPad
  25. #define MAX_CACHE_SIZE_WATCH 15 // three times the number of items shown on 42mm Watch
  26. +(void)initialize
  27. {
  28. _currentDeviceIdiom = [[UIDevice currentDevice] userInterfaceIdiom];
  29. MaxCacheSize = 0;
  30. switch (_currentDeviceIdiom) {
  31. case UIUserInterfaceIdiomPad:
  32. MaxCacheSize = MAX_CACHE_SIZE_IPAD;
  33. break;
  34. case UIUserInterfaceIdiomPhone:
  35. MaxCacheSize = MAX_CACHE_SIZE_IPHONE;
  36. break;
  37. default:
  38. MaxCacheSize = MAX_CACHE_SIZE_WATCH;
  39. break;
  40. }
  41. _thumbnailCache = [[NSCache alloc] init];
  42. _thumbnailCacheMetadata = [[NSCache alloc] init];
  43. [_thumbnailCache setCountLimit: MaxCacheSize];
  44. [_thumbnailCacheMetadata setCountLimit: MaxCacheSize];
  45. }
  46. - (NSString *)_md5FromString:(NSString *)string
  47. {
  48. const char *ptr = [string UTF8String];
  49. unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
  50. CC_MD5(ptr, (unsigned int)strlen(ptr), md5Buffer);
  51. NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
  52. for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
  53. [output appendFormat:@"%02x",md5Buffer[i]];
  54. return [NSString stringWithString:output];
  55. }
  56. + (UIImage *)thumbnailForMediaItemWithTitle:(NSString *)title Artist:(NSString*)artist andAlbumName:(NSString*)albumname
  57. {
  58. return [UIImage imageWithContentsOfFile:[[VLCThumbnailsCache new] artworkPathForMediaItemWithTitle:title Artist:artist andAlbumName:albumname]];
  59. }
  60. - (NSString *)artworkPathForMediaItemWithTitle:(NSString *)title Artist:(NSString*)artist andAlbumName:(NSString*)albumname
  61. {
  62. NSString *artworkURL;
  63. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  64. NSString *cacheDir = searchPaths[0];
  65. cacheDir = [cacheDir stringByAppendingFormat:@"/%@", [[NSBundle mainBundle] bundleIdentifier]];
  66. if (artist.length == 0 || albumname.length == 0) {
  67. /* Use generated hash to find art */
  68. artworkURL = [cacheDir stringByAppendingFormat:@"/art/arturl/%@/art.jpg", [self _md5FromString:title]];
  69. } else {
  70. /* Otherwise, it was cached by artist and album */
  71. artworkURL = [cacheDir stringByAppendingFormat:@"/art/artistalbum/%@/%@/art.jpg", artist, albumname];
  72. }
  73. return artworkURL;
  74. }
  75. - (NSString *)_getArtworkPathFromMedia:(MLFile *)file
  76. {
  77. NSString *artist, *album, *title;
  78. if (file.isAlbumTrack) {
  79. artist = file.albumTrack.artist;
  80. album = file.albumTrack.album.name;
  81. }
  82. title = file.title;
  83. return [self artworkPathForMediaItemWithTitle:title Artist:artist andAlbumName:album];
  84. }
  85. + (UIImage *)thumbnailForManagedObject:(NSManagedObject *)object
  86. {
  87. UIImage *thumbnail;
  88. VLCThumbnailsCache *cache = [VLCThumbnailsCache new];
  89. if ([object isKindOfClass:[MLShow class]]) {
  90. thumbnail = [cache thumbnailForShow:(MLShow *)object];
  91. } else if ([object isKindOfClass:[MLShowEpisode class]]) {
  92. MLFile *anyFileFromEpisode = [(MLShowEpisode *)object files].anyObject;
  93. thumbnail = [cache thumbnailForMediaFile:anyFileFromEpisode];
  94. } else if ([object isKindOfClass:[MLLabel class]]) {
  95. thumbnail = [cache thumbnailForLabel:(MLLabel *)object];
  96. } else if ([object isKindOfClass:[MLAlbum class]]) {
  97. MLFile *anyFileFromAnyTrack = [[(MLAlbum *)object tracks].anyObject files].anyObject;
  98. thumbnail = [cache thumbnailForMediaFile:anyFileFromAnyTrack];
  99. } else if ([object isKindOfClass:[MLAlbumTrack class]]) {
  100. MLFile *anyFileFromTrack = [(MLAlbumTrack *)object files].anyObject;
  101. thumbnail = [cache thumbnailForMediaFile:anyFileFromTrack];
  102. } else {
  103. thumbnail = [cache thumbnailForMediaFile:(MLFile *)object];
  104. }
  105. return thumbnail;
  106. }
  107. - (UIImage *)thumbnailForMediaFile:(MLFile *)mediaFile
  108. {
  109. if (mediaFile == nil || mediaFile.objectID == nil)
  110. return nil;
  111. NSManagedObjectID *objID = mediaFile.objectID;
  112. UIImage *displayedImage = [_thumbnailCache objectForKey:objID];
  113. if (displayedImage)
  114. return displayedImage;
  115. if (mediaFile.isAlbumTrack || mediaFile.isShowEpisode)
  116. displayedImage = [UIImage imageWithContentsOfFile:[self _getArtworkPathFromMedia:mediaFile]];
  117. if (!displayedImage)
  118. displayedImage = mediaFile.computedThumbnail;
  119. if (displayedImage)
  120. [_thumbnailCache setObject:displayedImage forKey:objID];
  121. return displayedImage;
  122. }
  123. - (UIImage *)thumbnailForShow:(MLShow *)mediaShow
  124. {
  125. NSManagedObjectID *objID = mediaShow.objectID;
  126. UIImage *displayedImage;
  127. BOOL forceRefresh = NO;
  128. NSUInteger count = [mediaShow.episodes count];
  129. NSNumber *previousCount = [_thumbnailCacheMetadata objectForKey:objID];
  130. if (previousCount.unsignedIntegerValue != count)
  131. forceRefresh = YES;
  132. if (!forceRefresh) {
  133. displayedImage = [_thumbnailCache objectForKey:objID];
  134. if (displayedImage)
  135. return displayedImage;
  136. }
  137. NSUInteger fileNumber = count > 3 ? 3 : count;
  138. NSArray *episodes = [mediaShow.episodes allObjects];
  139. NSMutableArray *files = [[NSMutableArray alloc] init];
  140. for (NSUInteger x = 0; x < count; x++) {
  141. /* this is a multi-threaded app, so the episode object might be there already,
  142. * but without an assigned file, so we need to check for its existance (#13128) */
  143. if ([episodes[x] files].anyObject != nil)
  144. [files addObject:[episodes[x] files].anyObject];
  145. }
  146. displayedImage = [self clusterThumbFromFiles:files andNumber:fileNumber blur:NO];
  147. if (displayedImage) {
  148. [_thumbnailCache setObject:displayedImage forKey:objID];
  149. [_thumbnailCacheMetadata setObject:@(count) forKey:objID];
  150. }
  151. return displayedImage;
  152. }
  153. - (UIImage *)thumbnailForLabel:(MLLabel *)mediaLabel
  154. {
  155. NSManagedObjectID *objID = mediaLabel.objectID;
  156. UIImage *displayedImage;
  157. BOOL forceRefresh = NO;
  158. NSUInteger count = [mediaLabel.files count];
  159. NSNumber *previousCount = [_thumbnailCacheMetadata objectForKey:objID];
  160. if (previousCount.unsignedIntegerValue != count)
  161. forceRefresh = YES;
  162. if (!forceRefresh) {
  163. displayedImage = [_thumbnailCache objectForKey:objID];
  164. if (displayedImage)
  165. return displayedImage;
  166. }
  167. NSUInteger fileNumber = count > 3 ? 3 : count;
  168. NSArray *files = [mediaLabel.files allObjects];
  169. BOOL blur = NO;
  170. if (SYSTEM_RUNS_IOS7_OR_LATER)
  171. blur = YES;
  172. displayedImage = [self clusterThumbFromFiles:files andNumber:fileNumber blur:blur];
  173. if (displayedImage) {
  174. [_thumbnailCache setObject:displayedImage forKey:objID];
  175. [_thumbnailCacheMetadata setObject:@(count) forKey:objID];
  176. }
  177. return displayedImage;
  178. }
  179. - (UIImage *)clusterThumbFromFiles:(NSArray *)files andNumber:(NSUInteger)fileNumber blur:(BOOL)blurImage
  180. {
  181. UIImage *clusterThumb;
  182. CGSize imageSize;
  183. if (_currentDeviceIdiom == UIUserInterfaceIdiomPad) {
  184. if ([UIScreen mainScreen].scale==2.0)
  185. imageSize = CGSizeMake(682., 384.);
  186. else
  187. imageSize = CGSizeMake(341., 192.);
  188. } else if (_currentDeviceIdiom == UIUserInterfaceIdiomPhone) {
  189. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  190. if ([UIScreen mainScreen].scale==2.0)
  191. imageSize = CGSizeMake(480., 270.);
  192. else
  193. imageSize = CGSizeMake(720., 405.);
  194. } else {
  195. if ([UIScreen mainScreen].scale==2.0)
  196. imageSize = CGSizeMake(258., 145.);
  197. else
  198. imageSize = CGSizeMake(129., 73.);
  199. }
  200. } else {
  201. imageSize = CGSizeMake(272., 120.);
  202. }
  203. UIGraphicsBeginImageContext(imageSize);
  204. NSUInteger iter = files.count < fileNumber ? files.count : fileNumber;
  205. for (NSUInteger i = 0; i < iter; i++) {
  206. MLFile *file = [files objectAtIndex:i];
  207. clusterThumb = [self thumbnailForMediaFile:file];
  208. CGContextRef context = UIGraphicsGetCurrentContext();
  209. CGFloat imagePartWidth = (imageSize.width / iter);
  210. //the rect in which the image should be drawn
  211. CGRect clippingRect = CGRectMake(imagePartWidth * i, 0, imagePartWidth, imageSize.height);
  212. CGContextSaveGState(context);
  213. CGContextClipToRect(context, clippingRect);
  214. //take the center of the clippingRect and calculate the offset from the original center
  215. CGFloat centerOffset = (imagePartWidth * i + imagePartWidth / 2) - imageSize.width / 2;
  216. //shift the rect to draw the middle of the image in the clippingrect
  217. CGRect drawingRect = CGRectMake(centerOffset, 0, imageSize.width, imageSize.height);
  218. [clusterThumb drawInRect:drawingRect];
  219. //get rid of the old clippingRect
  220. CGContextRestoreGState(context);
  221. }
  222. clusterThumb = UIGraphicsGetImageFromCurrentImageContext();
  223. UIGraphicsEndImageContext();
  224. if (!blurImage)
  225. return clusterThumb;
  226. return [UIImage applyBlurOnImage:clusterThumb withRadius:0.1];
  227. }
  228. @end