浏览代码

thumbnail cache: simplify OS flavor and device switching, add support for watchOS 2

Felix Paul Kühne 10 年之前
父节点
当前提交
62c1245454
共有 2 个文件被更改,包括 42 次插入37 次删除
  1. 40 35
      Sources/VLCThumbnailsCache.m
  2. 2 2
      VLC for iOS.xcodeproj/project.pbxproj

+ 40 - 35
Sources/VLCThumbnailsCache.m

@@ -22,10 +22,10 @@
 #import <MediaLibraryKit/MediaLibraryKit.h>
 
 @interface VLCThumbnailsCache() {
-    NSInteger MaxCacheSize;
+    NSInteger _maximalCacheSize;
     NSCache *_thumbnailCache;
     NSCache *_thumbnailCacheMetadata;
-    NSInteger _currentDeviceIdiom;
+    CGSize _imageSize;
 }
 @end
 
@@ -39,26 +39,50 @@
 {
     self = [super init];
     if (self) {
-        _currentDeviceIdiom = [[UIDevice currentDevice] userInterfaceIdiom];
-        MaxCacheSize = 0;
-
-        switch (_currentDeviceIdiom) {
+#if TARGET_OS_WATCH
+        CGRect screenRect = WKInterfaceDevice.currentDevice.screenBounds;
+        _imageSize = CGSizeMake(screenRect.size.width * WKInterfaceDevice.currentDevice.screenScale, 120.);
+        _maximalCacheSize = MAX_CACHE_SIZE_WATCH;
+#else
+        NSInteger currentDeviceIdiom = [[UIDevice currentDevice] userInterfaceIdiom];
+
+        switch (currentDeviceIdiom) {
             case UIUserInterfaceIdiomPad:
-                MaxCacheSize = MAX_CACHE_SIZE_IPAD;
+                _maximalCacheSize = MAX_CACHE_SIZE_IPAD;
                 break;
             case UIUserInterfaceIdiomPhone:
-                MaxCacheSize = MAX_CACHE_SIZE_IPHONE;
+                _maximalCacheSize = MAX_CACHE_SIZE_IPHONE;
                 break;
 
             default:
-                MaxCacheSize = MAX_CACHE_SIZE_WATCH;
+                _maximalCacheSize = MAX_CACHE_SIZE_WATCH;
                 break;
         }
 
+        if (currentDeviceIdiom == UIUserInterfaceIdiomPad) {
+            if ([UIScreen mainScreen].scale==2.0)
+                _imageSize = CGSizeMake(682., 384.);
+            else
+                _imageSize = CGSizeMake(341., 192.);
+        } else if (currentDeviceIdiom == UIUserInterfaceIdiomPhone) {
+            if ([UIScreen mainScreen].scale==2.0)
+                _imageSize = CGSizeMake(480., 270.);
+            else
+                _imageSize = CGSizeMake(720., 405.);
+        } else {
+            if ([WKInterfaceDevice class]) {
+                if (WKInterfaceDevice.currentDevice != nil) {
+                    CGRect screenRect = WKInterfaceDevice.currentDevice.screenBounds;
+                    _imageSize = CGSizeMake(screenRect.size.width * WKInterfaceDevice.currentDevice.screenScale, 120.);
+                }
+            }
+        }
+#endif
+
         _thumbnailCache = [[NSCache alloc] init];
         _thumbnailCacheMetadata = [[NSCache alloc] init];
-        [_thumbnailCache setCountLimit: MaxCacheSize];
-        [_thumbnailCacheMetadata setCountLimit: MaxCacheSize];
+        [_thumbnailCache setCountLimit: _maximalCacheSize];
+        [_thumbnailCacheMetadata setCountLimit: _maximalCacheSize];
     }
     return self;
 }
@@ -259,41 +283,22 @@
 - (UIImage *)clusterThumbFromFiles:(NSArray *)files andNumber:(NSUInteger)fileNumber blur:(BOOL)blurImage
 {
     UIImage *clusterThumb;
-    CGSize imageSize;
-    if (_currentDeviceIdiom == UIUserInterfaceIdiomPad) {
-        if ([UIScreen mainScreen].scale==2.0)
-            imageSize = CGSizeMake(682., 384.);
-        else
-            imageSize = CGSizeMake(341., 192.);
-    } else if (_currentDeviceIdiom == UIUserInterfaceIdiomPhone) {
-        if ([UIScreen mainScreen].scale==2.0)
-            imageSize = CGSizeMake(480., 270.);
-        else
-            imageSize = CGSizeMake(720., 405.);
-    } else {
-        if (SYSTEM_RUNS_IOS82_OR_LATER) {
-            if (WKInterfaceDevice.currentDevice != nil) {
-                CGRect screenRect = WKInterfaceDevice.currentDevice.screenBounds;
-                imageSize = CGSizeMake(screenRect.size.width * WKInterfaceDevice.currentDevice.screenScale, 120.);
-            }
-        }
-    }
 
-    UIGraphicsBeginImageContext(imageSize);
+    UIGraphicsBeginImageContext(_imageSize);
     NSUInteger iter = files.count < fileNumber ? files.count : fileNumber;
     for (NSUInteger i = 0; i < iter; i++) {
         MLFile *file =  [files objectAtIndex:i];
         clusterThumb = [self thumbnailForMediaFile:file refreshCache:NO];
         CGContextRef context = UIGraphicsGetCurrentContext();
-        CGFloat imagePartWidth = (imageSize.width / iter);
+        CGFloat imagePartWidth = (_imageSize.width / iter);
         //the rect in which the image should be drawn
-        CGRect clippingRect = CGRectMake(imagePartWidth * i, 0, imagePartWidth, imageSize.height);
+        CGRect clippingRect = CGRectMake(imagePartWidth * i, 0, imagePartWidth, _imageSize.height);
         CGContextSaveGState(context);
         CGContextClipToRect(context, clippingRect);
         //take the center of the clippingRect and calculate the offset from the original center
-        CGFloat centerOffset = (imagePartWidth * i + imagePartWidth / 2) - imageSize.width / 2;
+        CGFloat centerOffset = (imagePartWidth * i + imagePartWidth / 2) - _imageSize.width / 2;
         //shift the rect to draw the middle of the image in the clippingrect
-        CGRect drawingRect = CGRectMake(centerOffset, 0, imageSize.width, imageSize.height);
+        CGRect drawingRect = CGRectMake(centerOffset, 0, _imageSize.width, _imageSize.height);
         [clusterThumb drawInRect:drawingRect];
         //get rid of the old clippingRect
         CGContextRestoreGState(context);

+ 2 - 2
VLC for iOS.xcodeproj/project.pbxproj

@@ -1160,8 +1160,6 @@
 				7D378498183A98D1009EE944 /* VLCLibraryViewController.m */,
 				8F91EC77195CEC7900F5BCBA /* VLCOpenInActivity.h */,
 				8F91EC78195CEC7900F5BCBA /* VLCOpenInActivity.m */,
-				7D37849C183A98DD009EE944 /* VLCThumbnailsCache.h */,
-				7D37849D183A98DD009EE944 /* VLCThumbnailsCache.m */,
 			);
 			name = "Everything Playlist";
 			sourceTree = "<group>";
@@ -1618,6 +1616,8 @@
 		DD7110ED1AF38AFD00854776 /* SharedSources */ = {
 			isa = PBXGroup;
 			children = (
+				7D37849C183A98DD009EE944 /* VLCThumbnailsCache.h */,
+				7D37849D183A98DD009EE944 /* VLCThumbnailsCache.m */,
 				DD7110EE1AF38B2B00854776 /* MLMediaLibrary+playlist.h */,
 				DD7110EF1AF38B2B00854776 /* MLMediaLibrary+playlist.m */,
 				DD3EA62F1AF50CFE007FF096 /* VLCWatchMessage.h */,