UIImage+MLKit.m 3.01 KB
Newer Older
1 2 3 4 5 6 7 8 9
//
//  UIImage.m
//  MediaLibraryKit
//
//  Created by Felix Paul Kühne on 29/05/15.
//  Copyright (c) 2015 VideoLAN. All rights reserved.
//

#import "UIImage+MLKit.h"
10
#import "TargetConditionals.h"
11

12 13 14
#if TARGET_OS_WATCH
#import <WatchKit/WatchKit.h>
#endif
15 16 17 18
@implementation UIImage (MLKit)

+ (CGSize)preferredThumbnailSizeForDevice
{
19
#if TARGET_OS_IOS
20 21 22
    CGFloat thumbnailWidth, thumbnailHeight;
    /* optimize thumbnails for the device */
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
23 24
        thumbnailWidth = 272.;
        thumbnailHeight = 204.;
25
    } else {
26 27
        thumbnailWidth = 240.;
        thumbnailHeight = 135.;
28 29
    }
    return CGSizeMake(thumbnailWidth, thumbnailHeight);
30 31 32 33
#elif TARGET_OS_WATCH
    return [WKInterfaceDevice currentDevice].screenBounds.size;
#endif
    return CGSizeZero;
34 35
}

36
+ (UIImage *)scaleImage:(UIImage *)image toFitRect:(CGRect)rect {
37 38 39 40 41 42 43
    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];
44 45
}

46 47 48 49 50 51 52 53 54 55 56 57
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;
}


58
+ (UIImage *)scaleImage:(UIImage *)image toFitRect:(CGRect)rect scale:(CGFloat)scale
59
{
60 61 62 63
    CGRect destinationRect = MakeRectWithAspectRatioInsideRect(image.size, rect);


    destinationRect = CGRectIntegral(CGRectMake(destinationRect.origin.x, destinationRect.origin.y, destinationRect.size.width*scale, destinationRect.size.height*scale));
64 65 66 67 68 69 70 71

    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,
Tobias's avatar
Tobias committed
72 73
                                                    destinationRect.size.width,
                                                    destinationRect.size.height,
74 75 76 77 78 79 80 81
                                                    bitsPerComponent,
                                                    bytesPerRow,
                                                    colorSpaceRef,
                                                    bitmapInfoRef);

    CGContextSetInterpolationQuality(contextRef, kCGInterpolationLow);

    CGContextDrawImage(contextRef, (CGRect){CGPointZero, destinationRect.size}, cgImage);
Tobias's avatar
Tobias committed
82
    CGImageRef imageRef = CGBitmapContextCreateImage(contextRef);
83
    UIImage *scaledImage = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
Tobias's avatar
Tobias committed
84 85 86
    CGImageRelease(imageRef);
    CGContextRelease(contextRef);
    return scaledImage;
87 88 89
}

@end