瀏覽代碼

library: optimize folder thumbnailer so we don't need to re-create the split imagery on every draw event. add support for retina displays

Felix Paul Kühne 11 年之前
父節點
當前提交
05976aed08
共有 3 個文件被更改,包括 46 次插入29 次删除
  1. 3 29
      Sources/VLCPlaylistCollectionViewCell.m
  2. 2 0
      Sources/VLCThumbnailsCache.h
  3. 41 0
      Sources/VLCThumbnailsCache.m

+ 3 - 29
Sources/VLCPlaylistCollectionViewCell.m

@@ -140,36 +140,10 @@
         [self _configureForFolder:mediaObject];
 
         if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
-
-            NSUInteger fileNumber = [mediaObject.files count] > 3 ? 3 : [mediaObject.files count];
-            if (fileNumber == 0) {
+            if (mediaObject.files.count == 0)
                 self.thumbnailView.image = [UIImage imageNamed:@"folderIcon"];
-            } else {
-                NSArray *files = [mediaObject.files allObjects];
-                CGSize frameSize = self.thumbnailView.frame.size;
-                UIImage *displayedImage;
-                UIGraphicsBeginImageContext(frameSize);
-                for (NSUInteger i = 0; i < fileNumber; i++) {
-                    MLFile *file =  [files objectAtIndex:i];
-                    displayedImage = [VLCThumbnailsCache thumbnailForMediaFile:file];
-                    CGContextRef context = UIGraphicsGetCurrentContext();
-                    CGFloat imagePartWidth = (frameSize.width / fileNumber);
-                    //the rect in which the image should be drawn
-                    CGRect clippingRect = CGRectMake(imagePartWidth * i, 0, imagePartWidth, frameSize.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) - frameSize.width / 2;
-                    //shift the rect to draw the middle of the image in the clippingrect
-                    CGRect drawingRect = CGRectMake(centerOffset, 0, frameSize.width, frameSize.height);
-                    [displayedImage drawInRect:drawingRect];
-                    //get rid of the old clippingRect
-                    CGContextRestoreGState(context);
-                }
-                displayedImage = UIGraphicsGetImageFromCurrentImageContext();
-                UIGraphicsEndImageContext();
-                self.thumbnailView.image = displayedImage;
-            }
+            else
+                self.thumbnailView.image = [VLCThumbnailsCache thumbnailForLabel:mediaObject ofSize:self.thumbnailView.frame.size];
         }
     } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
         MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;

+ 2 - 0
Sources/VLCThumbnailsCache.h

@@ -19,4 +19,6 @@
 
 + (UIImage *)thumbnailForMediaItemWithTitle:(NSString *)title Artist:(NSString*)artist andAlbumName:(NSString*)albumname;
 
++ (UIImage *)thumbnailForLabel:(MLLabel *)mediaLabel ofSize:(CGSize)size;
+
 @end

+ 41 - 0
Sources/VLCThumbnailsCache.m

@@ -102,4 +102,45 @@ static NSCache *_thumbnailCache;
     return displayedImage;
 }
 
++ (UIImage *)thumbnailForLabel:(MLLabel *)mediaLabel ofSize:(CGSize)imageSize
+{
+    NSManagedObjectID *objID = mediaLabel.objectID;
+    UIImage *displayedImage = [_thumbnailCache objectForKey:objID];
+
+    if (displayedImage)
+        return displayedImage;
+
+    NSUInteger fileNumber = [mediaLabel.files count] > 3 ? 3 : [mediaLabel.files count];
+    NSArray *files = [mediaLabel.files allObjects];
+
+    /* for retina displays, our image size needs to be twice as big */
+    if ([UIScreen mainScreen].scale == 2.)
+        imageSize = CGSizeMake(imageSize.width * 2., imageSize.height * 2.);
+
+    UIGraphicsBeginImageContext(imageSize);
+    for (NSUInteger i = 0; i < fileNumber; i++) {
+        MLFile *file =  [files objectAtIndex:i];
+        displayedImage = [VLCThumbnailsCache thumbnailForMediaFile:file];
+        CGContextRef context = UIGraphicsGetCurrentContext();
+        CGFloat imagePartWidth = (imageSize.width / fileNumber);
+        //the rect in which the image should be drawn
+        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;
+        //shift the rect to draw the middle of the image in the clippingrect
+        CGRect drawingRect = CGRectMake(centerOffset, 0, imageSize.width, imageSize.height);
+        [displayedImage drawInRect:drawingRect];
+        //get rid of the old clippingRect
+        CGContextRestoreGState(context);
+    }
+    displayedImage = UIGraphicsGetImageFromCurrentImageContext();
+    UIGraphicsEndImageContext();
+
+    [_thumbnailCache setObject:displayedImage forKey:objID];
+
+    return displayedImage;
+}
+
 @end