VLCThumbnailsCache.m 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //
  2. // VLCThumbnailsCache.m
  3. // VLC for iOS
  4. //
  5. // Created by Gleb on 9/13/13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCThumbnailsCache.h"
  11. static NSInteger MaxCacheSize;
  12. static NSCache *_thumbnailCache;
  13. @implementation VLCThumbnailsCache
  14. #define MAX_CACHE_SIZE_IPHONE 21 // three times the number of items shown on iPhone 5
  15. #define MAX_CACHE_SIZE_IPAD 27 // three times the number of items shown on iPad
  16. +(void)initialize
  17. {
  18. MaxCacheSize = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)?
  19. MAX_CACHE_SIZE_IPAD: MAX_CACHE_SIZE_IPHONE;
  20. _thumbnailCache = [[NSCache alloc] init];
  21. [_thumbnailCache setCountLimit: MaxCacheSize];
  22. }
  23. + (UIImage *)thumbnailForMediaFile:(MLFile *)mediaFile
  24. {
  25. if (mediaFile == nil || mediaFile.objectID == nil)
  26. return nil;
  27. NSManagedObjectID *objID = mediaFile.objectID;
  28. UIImage *displayedImage = [_thumbnailCache objectForKey:objID];
  29. if (displayedImage)
  30. return displayedImage;
  31. displayedImage = mediaFile.computedThumbnail;
  32. if (displayedImage)
  33. [_thumbnailCache setObject:displayedImage forKey:objID];
  34. return displayedImage;
  35. }
  36. @end