Commit b9a08fbb authored by Felix Paul Kühne's avatar Felix Paul Kühne

optimize stored album artwork size for device

parent 787fca87
......@@ -32,6 +32,8 @@
#import "MLAlbum.h"
#import "MLTitleDecrapifier.h"
#import <CommonCrypto/CommonDigest.h> // for MD5
#import <AVFoundation/AVFoundation.h>
#import "MLThumbnailerQueue.h"
@interface MLFileParserQueue ()
{
......@@ -167,7 +169,8 @@
albumName = file.albumTrack.album.name;
}
title = file.title;
file.computedThumbnail = [UIImage imageWithContentsOfFile:[self artworkPathForMediaItemWithTitle:title Artist:artist andAlbumName:albumName]];
file.computedThumbnail = [self scaleImage:[UIImage imageWithContentsOfFile:[self artworkPathForMediaItemWithTitle:title Artist:artist andAlbumName:albumName]]
toFitRect:(CGRect){CGPointZero, [[MLThumbnailerQueue sharedThumbnailerQueue] preferredThumbnailSizeForDevice]}];
}
MLFileParserQueue *parserQueue = [MLFileParserQueue sharedFileParserQueue];
......@@ -180,6 +183,31 @@
#pragma mark - audio file specific code
- (UIImage *)scaleImage:(UIImage *)image toFitRect:(CGRect)rect
{
CGRect destinationRect = AVMakeRectWithAspectRatioInsideRect(image.size, rect);
CGImageRef cgImage = image.CGImage;
size_t bitsPerComponent = CGImageGetBitsPerComponent(cgImage);
size_t bytesPerRow = CGImageGetBytesPerRow(cgImage);
CGColorSpaceRef colorSpaceRef = CGImageGetColorSpace(cgImage);
CGBitmapInfo bitmapInfoRef = CGImageGetBitmapInfo(cgImage);
CGContextRef contextRef = CGBitmapContextCreate(NULL,
destinationRect.size.width,
destinationRect.size.height,
bitsPerComponent,
bytesPerRow,
colorSpaceRef,
bitmapInfoRef);
CGContextSetInterpolationQuality(contextRef, kCGInterpolationLow);
CGContextDrawImage(contextRef, (CGRect){CGPointZero, destinationRect.size}, cgImage);
return [UIImage imageWithCGImage:CGBitmapContextCreateImage(contextRef)];
}
- (NSString *)artworkPathForMediaItemWithTitle:(NSString *)title Artist:(NSString*)artist andAlbumName:(NSString*)albumname
{
NSString *artworkURL;
......
......@@ -38,5 +38,7 @@
- (void)stop;
- (void)resume;
- (CGSize)preferredThumbnailSizeForDevice;
@property (nonatomic, strong) NSOperationQueue *queue;
@end
......@@ -58,7 +58,6 @@
return self;
}
- (void)fetchThumbnail
{
APLog(@"Starting THUMB %@", self.file);
......@@ -67,36 +66,13 @@
_media = [VLCMedia mediaWithURL:[NSURL URLWithString:self.file.url]];
VLCMediaThumbnailer *thumbnailer = [VLCMediaThumbnailer thumbnailerWithMedia:_media delegate:self andVLCLibrary:_internalLibrary];
/* optimize thumbnails for the device */
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if ([UIScreen mainScreen].scale==2.0) {
thumbnailer.thumbnailWidth = 540.;
thumbnailer.thumbnailHeight = 405.;
} else {
thumbnailer.thumbnailWidth = 272.;
thumbnailer.thumbnailHeight = 204.;
}
} else {
if (SYSTEM_RUNS_IOS7) {
if ([UIScreen mainScreen].scale==2.0) {
thumbnailer.thumbnailWidth = 480.;
thumbnailer.thumbnailHeight = 270.;
} else {
thumbnailer.thumbnailWidth = 720.;
thumbnailer.thumbnailHeight = 405.;
}
} else {
if ([UIScreen mainScreen].scale==2.0) {
thumbnailer.thumbnailWidth = 480.;
thumbnailer.thumbnailHeight = 270.;
} else {
thumbnailer.thumbnailWidth = 240.;
thumbnailer.thumbnailHeight = 135.;
}
}
}
MLThumbnailerQueue *thumbnailerQueue = [MLThumbnailerQueue sharedThumbnailerQueue];
CGSize thumbSize = [thumbnailerQueue preferredThumbnailSizeForDevice];
thumbnailer.thumbnailWidth = thumbSize.width;
thumbnailer.thumbnailHeight = thumbSize.height;
[thumbnailer fetchThumbnail];
[[MLThumbnailerQueue sharedThumbnailerQueue].queue setSuspended:YES]; // Balanced in -mediaThumbnailer:didFinishThumbnail
[thumbnailerQueue.queue setSuspended:YES]; // Balanced in -mediaThumbnailer:didFinishThumbnail
// Balanced in -mediaThumbnailer:didFinishThumbnail:
}
- (void)main
......@@ -107,9 +83,9 @@
- (void)endThumbnailing
{
[[MLCrashPreventer sharedPreventer] didParseFile:self.file];
MLThumbnailerQueue *thumbnailer = [MLThumbnailerQueue sharedThumbnailerQueue];
[thumbnailer.queue setSuspended:NO];
[thumbnailer didFinishOperation:self];
MLThumbnailerQueue *thumbnailerQueue = [MLThumbnailerQueue sharedThumbnailerQueue];
[thumbnailerQueue.queue setSuspended:NO];
[thumbnailerQueue didFinishOperation:self];
}
- (void)mediaThumbnailer:(VLCMediaThumbnailer *)mediaThumbnailer didFinishThumbnail:(CGImageRef)thumbnail
{
......@@ -155,13 +131,45 @@
return self;
}
static inline NSString *hashFromFile(MLFile *file)
{
return [NSString stringWithFormat:@"%p", [[file objectID] URIRepresentation]];
}
- (CGSize)preferredThumbnailSizeForDevice
{
CGFloat thumbnailWidth, thumbnailHeight;
/* optimize thumbnails for the device */
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if ([UIScreen mainScreen].scale==2.0) {
thumbnailWidth = 540.;
thumbnailHeight = 405.;
} else {
thumbnailWidth = 272.;
thumbnailHeight = 204.;
}
} else {
if (SYSTEM_RUNS_IOS7) {
if ([UIScreen mainScreen].scale==2.0) {
thumbnailWidth = 480.;
thumbnailHeight = 270.;
} else {
thumbnailWidth = 720.;
thumbnailHeight = 405.;
}
} else {
if ([UIScreen mainScreen].scale==2.0) {
thumbnailWidth = 480.;
thumbnailHeight = 270.;
} else {
thumbnailWidth = 240.;
thumbnailHeight = 135.;
}
}
}
return CGSizeMake(thumbnailWidth, thumbnailHeight);
}
- (void)didFinishOperation:(ThumbnailOperation *)op
{
[_fileDescriptionToOperation setValue:nil forKey:hashFromFile(op.file)];
......
Markdown is supported
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