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 @@ ...@@ -32,6 +32,8 @@
#import "MLAlbum.h" #import "MLAlbum.h"
#import "MLTitleDecrapifier.h" #import "MLTitleDecrapifier.h"
#import <CommonCrypto/CommonDigest.h> // for MD5 #import <CommonCrypto/CommonDigest.h> // for MD5
#import <AVFoundation/AVFoundation.h>
#import "MLThumbnailerQueue.h"
@interface MLFileParserQueue () @interface MLFileParserQueue ()
{ {
...@@ -167,7 +169,8 @@ ...@@ -167,7 +169,8 @@
albumName = file.albumTrack.album.name; albumName = file.albumTrack.album.name;
} }
title = file.title; 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]; MLFileParserQueue *parserQueue = [MLFileParserQueue sharedFileParserQueue];
...@@ -180,6 +183,31 @@ ...@@ -180,6 +183,31 @@
#pragma mark - audio file specific code #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 *)artworkPathForMediaItemWithTitle:(NSString *)title Artist:(NSString*)artist andAlbumName:(NSString*)albumname
{ {
NSString *artworkURL; NSString *artworkURL;
......
...@@ -38,5 +38,7 @@ ...@@ -38,5 +38,7 @@
- (void)stop; - (void)stop;
- (void)resume; - (void)resume;
- (CGSize)preferredThumbnailSizeForDevice;
@property (nonatomic, strong) NSOperationQueue *queue; @property (nonatomic, strong) NSOperationQueue *queue;
@end @end
...@@ -58,7 +58,6 @@ ...@@ -58,7 +58,6 @@
return self; return self;
} }
- (void)fetchThumbnail - (void)fetchThumbnail
{ {
APLog(@"Starting THUMB %@", self.file); APLog(@"Starting THUMB %@", self.file);
...@@ -67,36 +66,13 @@ ...@@ -67,36 +66,13 @@
_media = [VLCMedia mediaWithURL:[NSURL URLWithString:self.file.url]]; _media = [VLCMedia mediaWithURL:[NSURL URLWithString:self.file.url]];
VLCMediaThumbnailer *thumbnailer = [VLCMediaThumbnailer thumbnailerWithMedia:_media delegate:self andVLCLibrary:_internalLibrary]; VLCMediaThumbnailer *thumbnailer = [VLCMediaThumbnailer thumbnailerWithMedia:_media delegate:self andVLCLibrary:_internalLibrary];
/* optimize thumbnails for the device */ MLThumbnailerQueue *thumbnailerQueue = [MLThumbnailerQueue sharedThumbnailerQueue];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if ([UIScreen mainScreen].scale==2.0) { CGSize thumbSize = [thumbnailerQueue preferredThumbnailSizeForDevice];
thumbnailer.thumbnailWidth = 540.; thumbnailer.thumbnailWidth = thumbSize.width;
thumbnailer.thumbnailHeight = 405.; thumbnailer.thumbnailHeight = thumbSize.height;
} 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.;
}
}
}
[thumbnailer fetchThumbnail]; [thumbnailer fetchThumbnail];
[[MLThumbnailerQueue sharedThumbnailerQueue].queue setSuspended:YES]; // Balanced in -mediaThumbnailer:didFinishThumbnail [thumbnailerQueue.queue setSuspended:YES]; // Balanced in -mediaThumbnailer:didFinishThumbnail
// Balanced in -mediaThumbnailer:didFinishThumbnail: // Balanced in -mediaThumbnailer:didFinishThumbnail:
} }
- (void)main - (void)main
...@@ -107,9 +83,9 @@ ...@@ -107,9 +83,9 @@
- (void)endThumbnailing - (void)endThumbnailing
{ {
[[MLCrashPreventer sharedPreventer] didParseFile:self.file]; [[MLCrashPreventer sharedPreventer] didParseFile:self.file];
MLThumbnailerQueue *thumbnailer = [MLThumbnailerQueue sharedThumbnailerQueue]; MLThumbnailerQueue *thumbnailerQueue = [MLThumbnailerQueue sharedThumbnailerQueue];
[thumbnailer.queue setSuspended:NO]; [thumbnailerQueue.queue setSuspended:NO];
[thumbnailer didFinishOperation:self]; [thumbnailerQueue didFinishOperation:self];
} }
- (void)mediaThumbnailer:(VLCMediaThumbnailer *)mediaThumbnailer didFinishThumbnail:(CGImageRef)thumbnail - (void)mediaThumbnailer:(VLCMediaThumbnailer *)mediaThumbnailer didFinishThumbnail:(CGImageRef)thumbnail
{ {
...@@ -155,13 +131,45 @@ ...@@ -155,13 +131,45 @@
return self; return self;
} }
static inline NSString *hashFromFile(MLFile *file) static inline NSString *hashFromFile(MLFile *file)
{ {
return [NSString stringWithFormat:@"%p", [[file objectID] URIRepresentation]]; 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 - (void)didFinishOperation:(ThumbnailOperation *)op
{ {
[_fileDescriptionToOperation setValue:nil forKey:hashFromFile(op.file)]; [_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