Commit 86072e81 authored by Tobias's avatar Tobias

make UIImage+MLKit available on watchOS

parent 9029a679
......@@ -28,6 +28,7 @@
#import <MediaLibraryKit/MLAlbum.h>
#import <MediaLibraryKit/MLAlbumTrack.h>
#import <MediaLibraryKit/MLThumbnailerQueue.h>
#import <MediaLibraryKit/UIImage+MLKit.h>
@class MLFile;
@class MLLabel;
......
......@@ -96,6 +96,7 @@
A792468E170F09A30036AAF2 /* MLThumbnailerQueue.h in Copy Files */ = {isa = PBXBuildFile; fileRef = 7D0EF521170885130003ED47 /* MLThumbnailerQueue.h */; };
DD9FBE711BA8883B00FFE77A /* UIImage+MLKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 7D201AC61B186724009AC64E /* UIImage+MLKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
DD9FBE721BA8891500FFE77A /* UIImage+MLKit.h in Copy Files */ = {isa = PBXBuildFile; fileRef = 7D201AC61B186724009AC64E /* UIImage+MLKit.h */; };
DD9FBE731BA985C600FFE77A /* UIImage+MLKit.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D201AC71B186724009AC64E /* UIImage+MLKit.m */; settings = {ASSET_TAGS = (); }; };
DDB959371AFB9B2B00BB8CFF /* MLMediaLibrary+Migration.m in Sources */ = {isa = PBXBuildFile; fileRef = DDB959361AFB9B2B00BB8CFF /* MLMediaLibrary+Migration.m */; };
DDB959391AFBAD8700BB8CFF /* MappingModel_2_5_to_2_6.xcmappingmodel in Sources */ = {isa = PBXBuildFile; fileRef = DDB959381AFBAD8700BB8CFF /* MappingModel_2_5_to_2_6.xcmappingmodel */; };
DDC57DFB1AFD0B5000DBAEF7 /* MLFileEntityMappingPolicy_2_5_to_2_6.m in Sources */ = {isa = PBXBuildFile; fileRef = DDC57DFA1AFD0B5000DBAEF7 /* MLFileEntityMappingPolicy_2_5_to_2_6.m */; };
......@@ -605,6 +606,7 @@
7D2DF7F21B677F3100FB78AB /* MLFileEntityMappingPolicy_2_5_to_2_6.m in Sources */,
7D2DF7F31B677F3100FB78AB /* MLThumbnailerQueue.m in Sources */,
7D2DF7F41B677F3100FB78AB /* MLTitleDecrapifier.m in Sources */,
DD9FBE731BA985C600FFE77A /* UIImage+MLKit.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......
......@@ -299,7 +299,7 @@ NSString *const MLFileThumbnailWasUpdated = @"MLFileThumbnailWasUpdated";
- (void)setComputedThumbnailScaledForDevice:(UIImage *)thumbnail
{
[self setComputedThumbnail:[UIImage scaleImage:thumbnail
toFitRect:(CGRect){CGPointZero, [UIImage preferredThumbnailSizeForDevice]}]];
toFitRect:(CGRect){CGPointZero, (CGSize)[UIImage preferredThumbnailSizeForDevice]}]];
}
- (void)setComputedThumbnail:(UIImage *)image
......
......@@ -29,6 +29,7 @@
#import "MLCrashPreventer.h"
#import "MLMediaLibrary.h"
#import "MLFileParserQueue.h"
#import "UIImage+MLKit.h"
#ifdef MLKIT_READONLY_TARGET
......
......@@ -7,12 +7,16 @@
//
#import "UIImage+MLKit.h"
#import <AVFoundation/AVFoundation.h>
#import "TargetConditionals.h"
#if TARGET_OS_WATCH
#import <WatchKit/WatchKit.h>
#endif
@implementation UIImage (MLKit)
+ (CGSize)preferredThumbnailSizeForDevice
{
#if TARGET_OS_IOS
CGFloat thumbnailWidth, thumbnailHeight;
/* optimize thumbnails for the device */
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
......@@ -23,16 +27,40 @@
thumbnailHeight = 135.;
}
return CGSizeMake(thumbnailWidth, thumbnailHeight);
#elif TARGET_OS_WATCH
return [WKInterfaceDevice currentDevice].screenBounds.size;
#endif
return CGSizeZero;
}
+ (UIImage *)scaleImage:(UIImage *)image toFitRect:(CGRect)rect {
return [self scaleImage:image toFitRect:rect scale:[UIScreen mainScreen].scale];
CGFloat scale = 0.0;
#if TARGET_OS_IOS
scale = [UIScreen mainScreen].scale;
#elif TARGET_OS_WATCH
scale = [WKInterfaceDevice currentDevice].screenScale;
#endif
return [self scaleImage:image toFitRect:rect scale:scale];
}
static inline CGRect MakeRectWithAspectRatioInsideRect(CGSize size, CGRect rect) {
CGFloat aspectWidth = rect.size.width/size.width;
CGFloat aspectHeight = rect.size.height/size.height;
CGFloat aspectRatio = MIN(aspectWidth, aspectHeight);
rect.size.width = ceill(size.width * aspectRatio);
rect.size.height = ceill(size.height * aspectRatio);
return rect;
}
+ (UIImage *)scaleImage:(UIImage *)image toFitRect:(CGRect)rect scale:(CGFloat)scale
{
CGRect destinationRect = AVMakeRectWithAspectRatioInsideRect(image.size, rect);
destinationRect = CGRectMake(destinationRect.origin.x, destinationRect.origin.y, destinationRect.size.width*scale, destinationRect.size.height*scale);
CGRect destinationRect = MakeRectWithAspectRatioInsideRect(image.size, rect);
destinationRect = CGRectIntegral(CGRectMake(destinationRect.origin.x, destinationRect.origin.y, destinationRect.size.width*scale, destinationRect.size.height*scale));
CGImageRef cgImage = image.CGImage;
size_t bitsPerComponent = CGImageGetBitsPerComponent(cgImage);
......
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