Commit 05976aed authored by Felix Paul Kühne's avatar Felix Paul Kühne
Browse files

library: optimize folder thumbnailer so we don't need to re-create the split...

library: optimize folder thumbnailer so we don't need to re-create the split imagery on every draw event. add support for retina displays
parent eccd2dd3
......@@ -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;
......
......@@ -19,4 +19,6 @@
+ (UIImage *)thumbnailForMediaItemWithTitle:(NSString *)title Artist:(NSString*)artist andAlbumName:(NSString*)albumname;
+ (UIImage *)thumbnailForLabel:(MLLabel *)mediaLabel ofSize:(CGSize)size;
@end
......@@ -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
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment