VLCThumbnailsCache.m 1.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
//
//  VLCThumbnailsCache.m
//  VLC for iOS
//
//  Created by Gleb on 9/13/13.
//  Copyright (c) 2013 VideoLAN. All rights reserved.
//
//  Refer to the COPYING file of the official project for license.
//

#import "VLCThumbnailsCache.h"

static NSInteger MaxCacheSize;
14
static NSCache *_thumbnailCache;
15 16 17 18 19 20 21 22 23 24 25

@implementation VLCThumbnailsCache

#define MAX_CACHE_SIZE_IPHONE 21  // three times the number of items shown on iPhone 5
#define MAX_CACHE_SIZE_IPAD   27  // three times the number of items shown on iPad

+(void)initialize
{
    MaxCacheSize = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)?
                                MAX_CACHE_SIZE_IPAD: MAX_CACHE_SIZE_IPHONE;

26 27
    _thumbnailCache = [[NSCache alloc] init];
    [_thumbnailCache setCountLimit: MaxCacheSize];
28 29 30 31 32 33 34 35
}

+ (UIImage *)thumbnailForMediaFile:(MLFile *)mediaFile
{
    if (mediaFile == nil || mediaFile.objectID == nil)
        return nil;

    NSManagedObjectID *objID = mediaFile.objectID;
36 37 38 39 40 41
    UIImage *displayedImage = [_thumbnailCache objectForKey:objID];

    if (displayedImage)
        return displayedImage;

    displayedImage = mediaFile.computedThumbnail;
42 43
    if (displayedImage)
        [_thumbnailCache setObject:displayedImage forKey:objID];
44 45 46 47 48

    return displayedImage;
}

@end