VLCThumbnailsCache.m 1.5 KB

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