فهرست منبع

generalize thumbnail clustering code to also use it with TV shows

Felix Paul Kühne 11 سال پیش
والد
کامیت
0f9f63e9e0
4فایلهای تغییر یافته به همراه40 افزوده شده و 11 حذف شده
  1. 1 2
      Sources/VLCPlaylistCollectionViewCell.m
  2. 1 2
      Sources/VLCPlaylistTableViewCell.m
  3. 1 0
      Sources/VLCThumbnailsCache.h
  4. 37 7
      Sources/VLCThumbnailsCache.m

+ 1 - 2
Sources/VLCPlaylistCollectionViewCell.m

@@ -166,8 +166,7 @@
         [self _configureForShow:mediaObject];
 
         if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
-            MLFile *anyFileFromAnyEpisode = [mediaObject.episodes.anyObject files].anyObject;
-            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyEpisode];
+            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForShow:mediaObject];
         }
     } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
         MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;

+ 1 - 2
Sources/VLCPlaylistTableViewCell.m

@@ -125,8 +125,7 @@
         [self _configureForShow:mediaObject];
 
         if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
-            MLFile *anyFileFromAnyEpisode = [mediaObject.episodes.anyObject files].anyObject;
-            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyEpisode];
+            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForShow:mediaObject];
         }
     } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
         MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;

+ 1 - 0
Sources/VLCThumbnailsCache.h

@@ -19,6 +19,7 @@
 
 + (UIImage *)thumbnailForMediaItemWithTitle:(NSString *)title Artist:(NSString*)artist andAlbumName:(NSString*)albumname;
 
++ (UIImage *)thumbnailForShow:(MLShow *)mediaShow;
 + (UIImage *)thumbnailForLabel:(MLLabel *)mediaLabel;
 
 @end

+ 37 - 7
Sources/VLCThumbnailsCache.m

@@ -102,6 +102,28 @@ static NSCache *_thumbnailCache;
     return displayedImage;
 }
 
++ (UIImage *)thumbnailForShow:(MLShow *)mediaShow
+{
+    NSManagedObjectID *objID = mediaShow.objectID;
+    UIImage *displayedImage = [_thumbnailCache objectForKey:objID];
+
+    if (displayedImage)
+        return displayedImage;
+
+    NSUInteger count = [mediaShow.episodes count];
+    NSUInteger fileNumber = count > 3 ? 3 : count;
+    NSArray *episodes = [mediaShow.episodes allObjects];
+    NSMutableArray *files = [[NSMutableArray alloc] init];
+    for (NSUInteger x = 0; x < count; x++)
+        [files addObject:[episodes[x] files].anyObject];
+
+    displayedImage = [self clusterThumbFromFiles:files andNumber:fileNumber];
+    if (displayedImage)
+        [_thumbnailCache setObject:displayedImage forKey:objID];
+
+    return displayedImage;
+}
+
 + (UIImage *)thumbnailForLabel:(MLLabel *)mediaLabel
 {
     NSManagedObjectID *objID = mediaLabel.objectID;
@@ -110,9 +132,19 @@ static NSCache *_thumbnailCache;
     if (displayedImage)
         return displayedImage;
 
-    NSUInteger fileNumber = [mediaLabel.files count] > 3 ? 3 : [mediaLabel.files count];
+    NSUInteger count = [mediaLabel.files count];
+    NSUInteger fileNumber = count > 3 ? 3 : count;
     NSArray *files = [mediaLabel.files allObjects];
+    displayedImage = [self clusterThumbFromFiles:files andNumber:fileNumber];
+    if (displayedImage)
+        [_thumbnailCache setObject:displayedImage forKey:objID];
+
+    return displayedImage;
+}
 
++ (UIImage *)clusterThumbFromFiles:(NSArray *)files andNumber:(NSUInteger)fileNumber
+{
+    UIImage *clusterThumb;
     CGSize imageSize;
     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
         if ([UIScreen mainScreen].scale==2.0)
@@ -133,7 +165,7 @@ static NSCache *_thumbnailCache;
     UIGraphicsBeginImageContext(imageSize);
     for (NSUInteger i = 0; i < fileNumber; i++) {
         MLFile *file =  [files objectAtIndex:i];
-        displayedImage = [VLCThumbnailsCache thumbnailForMediaFile:file];
+        clusterThumb = [VLCThumbnailsCache thumbnailForMediaFile:file];
         CGContextRef context = UIGraphicsGetCurrentContext();
         CGFloat imagePartWidth = (imageSize.width / fileNumber);
         //the rect in which the image should be drawn
@@ -144,16 +176,14 @@ static NSCache *_thumbnailCache;
         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];
+        [clusterThumb drawInRect:drawingRect];
         //get rid of the old clippingRect
         CGContextRestoreGState(context);
     }
-    displayedImage = UIGraphicsGetImageFromCurrentImageContext();
+    clusterThumb = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
 
-    [_thumbnailCache setObject:displayedImage forKey:objID];
-
-    return displayedImage;
+    return clusterThumb;
 }
 
 @end