Commit 48bd2067 authored by Felix Paul Kühne's avatar Felix Paul Kühne

refactor image scaling code

parent 73dea49a
......@@ -38,7 +38,5 @@
- (void)stop;
- (void)resume;
- (CGSize)preferredThumbnailSizeForDevice;
@property (nonatomic, strong) NSOperationQueue *queue;
@end
......@@ -33,6 +33,7 @@
7D0EF559170885230003ED47 /* CXMLNode_PrivateExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D0EF54A170885230003ED47 /* CXMLNode_PrivateExtensions.m */; };
7D0EF55A170885230003ED47 /* CXMLNode_XPathExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D0EF54C170885230003ED47 /* CXMLNode_XPathExtensions.m */; };
7D0EF55B170885230003ED47 /* CTidy.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D0EF54F170885230003ED47 /* CTidy.m */; };
7D201AC81B186724009AC64E /* UIImage+MLKit.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D201AC71B186724009AC64E /* UIImage+MLKit.m */; };
7D8ECFE21708FEAE00A989E9 /* CXHTMLDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D8ECFE11708FEAE00A989E9 /* CXHTMLDocument.m */; };
7D8ECFE51708FEC000A989E9 /* CXMLNamespaceNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D8ECFE41708FEC000A989E9 /* CXMLNamespaceNode.m */; };
7D9E238617AEEA13008485E5 /* MLAlbumTrack.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D9E238517AEEA13008485E5 /* MLAlbumTrack.m */; };
......@@ -131,6 +132,8 @@
7D0EF54E170885230003ED47 /* CTidy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CTidy.h; sourceTree = "<group>"; };
7D0EF54F170885230003ED47 /* CTidy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CTidy.m; sourceTree = "<group>"; };
7D0EF550170885230003ED47 /* TouchXML.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TouchXML.h; sourceTree = "<group>"; };
7D201AC61B186724009AC64E /* UIImage+MLKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+MLKit.h"; sourceTree = "<group>"; };
7D201AC71B186724009AC64E /* UIImage+MLKit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+MLKit.m"; sourceTree = "<group>"; };
7D8ECFE01708FEAE00A989E9 /* CXHTMLDocument.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CXHTMLDocument.h; sourceTree = "<group>"; };
7D8ECFE11708FEAE00A989E9 /* CXHTMLDocument.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CXHTMLDocument.m; sourceTree = "<group>"; };
7D8ECFE31708FEC000A989E9 /* CXMLNamespaceNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CXMLNamespaceNode.h; sourceTree = "<group>"; };
......@@ -265,6 +268,8 @@
7D0EF525170885130003ED47 /* MLTVShowInfoGrabber.m */,
7D0EF526170885130003ED47 /* MLURLConnection.m */,
7D0EF527170885130003ED47 /* NSXMLNode_Additions.m */,
7D201AC61B186724009AC64E /* UIImage+MLKit.h */,
7D201AC71B186724009AC64E /* UIImage+MLKit.m */,
);
path = Sources;
sourceTree = "<group>";
......@@ -402,6 +407,7 @@
7D0EF552170885230003ED47 /* CXMLNode_CreationExtensions.m in Sources */,
7D0EF553170885230003ED47 /* CXMLDocument.m in Sources */,
7D0EF554170885230003ED47 /* CXMLDocument_PrivateExtensions.m in Sources */,
7D201AC81B186724009AC64E /* UIImage+MLKit.m in Sources */,
7D0EF555170885230003ED47 /* CXMLElement.m in Sources */,
7D0EF556170885230003ED47 /* CXMLElement_CreationExtensions.m in Sources */,
7D0EF557170885230003ED47 /* CXMLElement_ElementTreeExtensions.m in Sources */,
......
......@@ -47,6 +47,7 @@
#endif
#import "NSXMLNode_Additions.h"
#import "UIImage+MLKit.h"
#ifndef NDEBUG
#define APLog(format, ...) NSLog(format, ## __VA_ARGS__)
......
......@@ -32,7 +32,6 @@
#import "MLAlbum.h"
#import "MLTitleDecrapifier.h"
#import <CommonCrypto/CommonDigest.h> // for MD5
#import <AVFoundation/AVFoundation.h>
#import "MLThumbnailerQueue.h"
@interface MLFileParserQueue ()
......@@ -185,8 +184,8 @@
NSString *artworkPath = [self artworkPathForMediaItemWithTitle:title Artist:artist andAlbumName:albumName];
if ([[NSFileManager defaultManager] fileExistsAtPath:artworkPath]) {
file.computedThumbnail = [self scaleImage:[UIImage imageWithContentsOfFile:artworkPath]
toFitRect:(CGRect){CGPointZero, [[MLThumbnailerQueue sharedThumbnailerQueue] preferredThumbnailSizeForDevice]}];
file.computedThumbnail = [UIImage scaleImage:[UIImage imageWithContentsOfFile:artworkPath]
toFitRect:(CGRect){CGPointZero, [UIImage preferredThumbnailSizeForDevice]}];
}
if (file.computedThumbnail == nil)
file.albumTrack.containsArtwork = NO;
......@@ -202,31 +201,6 @@
#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;
......
......@@ -68,7 +68,7 @@
VLCMediaThumbnailer *thumbnailer = [VLCMediaThumbnailer thumbnailerWithMedia:_media delegate:self andVLCLibrary:_internalLibrary];
MLThumbnailerQueue *thumbnailerQueue = [MLThumbnailerQueue sharedThumbnailerQueue];
CGSize thumbSize = [thumbnailerQueue preferredThumbnailSizeForDevice];
CGSize thumbSize = [UIImage preferredThumbnailSizeForDevice];
thumbnailer.thumbnailWidth = thumbSize.width;
thumbnailer.thumbnailHeight = thumbSize.height;
[thumbnailer fetchThumbnail];
......@@ -136,40 +136,6 @@ 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)];
......
//
// UIImage.h
// MediaLibraryKit
//
// Created by Felix Paul Kühne on 29/05/15.
// Copyright (c) 2015 VideoLAN. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (MLKit)
+ (CGSize)preferredThumbnailSizeForDevice;
+ (UIImage *)scaleImage:(UIImage *)image toFitRect:(CGRect)rect;
@end
//
// UIImage.m
// MediaLibraryKit
//
// Created by Felix Paul Kühne on 29/05/15.
// Copyright (c) 2015 VideoLAN. All rights reserved.
//
#import "UIImage+MLKit.h"
#import <AVFoundation/AVFoundation.h>
@implementation UIImage (MLKit)
+ (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);
}
+ (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)];
}
@end
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