Browse Source

thumbnail cache: add a way to fetch stuff at random sizes and if the caller believes that it should replace the cache copy, it can ask the cache to do so

Felix Paul Kühne 10 years ago
parent
commit
d71564a161
2 changed files with 26 additions and 1 deletions
  1. 3 1
      Sources/VLCThumbnailsCache.h
  2. 23 0
      Sources/VLCThumbnailsCache.m

+ 3 - 1
Sources/VLCThumbnailsCache.h

@@ -2,7 +2,7 @@
  * VLCThumbnailsCache.h
  * VLCThumbnailsCache.h
  * VLC for iOS
  * VLC for iOS
  *****************************************************************************
  *****************************************************************************
- * Copyright (c) 2013-2014 VideoLAN. All rights reserved.
+ * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  * $Id$
  * $Id$
  *
  *
  * Authors: Gleb Pinigin <gpinigin # gmail.com>
  * Authors: Gleb Pinigin <gpinigin # gmail.com>
@@ -18,4 +18,6 @@
 
 
 + (UIImage *)thumbnailForMediaItemWithTitle:(NSString *)title Artist:(NSString*)artist andAlbumName:(NSString*)albumname;
 + (UIImage *)thumbnailForMediaItemWithTitle:(NSString *)title Artist:(NSString*)artist andAlbumName:(NSString*)albumname;
 
 
++ (UIImage *)thumbnailForManagedObject:(NSManagedObject *)object toFitRect:(CGRect)rect shouldReplaceCache:(BOOL)replaceCache;
+
 @end
 @end

+ 23 - 0
Sources/VLCThumbnailsCache.m

@@ -16,6 +16,7 @@
 #import "VLCThumbnailsCache.h"
 #import "VLCThumbnailsCache.h"
 #import <CommonCrypto/CommonDigest.h>
 #import <CommonCrypto/CommonDigest.h>
 #import "UIImage+Blur.h"
 #import "UIImage+Blur.h"
+#import "UIImage+Scaling.h"
 #import <WatchKit/WatchKit.h>
 #import <WatchKit/WatchKit.h>
 
 
 @interface VLCThumbnailsCache() {
 @interface VLCThumbnailsCache() {
@@ -142,6 +143,28 @@
     return thumbnail;
     return thumbnail;
 }
 }
 
 
++ (UIImage *)thumbnailForManagedObject:(NSManagedObject *)object toFitRect:(CGRect)rect shouldReplaceCache:(BOOL)replaceCache
+{
+    UIImage *rawThumbnail = [self thumbnailForManagedObject:object];
+    CGSize rawSize = rawThumbnail.size;
+
+    /* scaling is potentially expensive, so we should avoid re-doing it for the same size over and over again */ 
+    if (rawSize.width <= rect.size.width && rawSize.height <= rect.size.height)
+        return rawThumbnail;
+
+    UIImage *scaledImage = [UIImage scaleImage:rawThumbnail toFitRect:rect];
+
+    if (replaceCache)
+        [[VLCThumbnailsCache sharedThumbnailCache] _setThumbnail:scaledImage forObjectId:object.objectID];
+
+    return scaledImage;
+}
+
+- (void)_setThumbnail:(UIImage *)image forObjectId:(NSManagedObjectID *)objID
+{
+    [_thumbnailCache setObject:image forKey:objID];
+}
+
 - (UIImage *)thumbnailForMediaFile:(MLFile *)mediaFile
 - (UIImage *)thumbnailForMediaFile:(MLFile *)mediaFile
 {
 {
     if (mediaFile == nil || mediaFile.objectID == nil)
     if (mediaFile == nil || mediaFile.objectID == nil)