|
|
@ -1,9 +1,5 @@
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// UIImage+normalizeImage.m
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
// Signal
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Created by Frederic Jacobs on 26/12/14.
|
|
|
|
|
|
|
|
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#import "UIImage+normalizeImage.h"
|
|
|
|
#import "UIImage+normalizeImage.h"
|
|
|
@ -11,8 +7,9 @@
|
|
|
|
@implementation UIImage (normalizeImage)
|
|
|
|
@implementation UIImage (normalizeImage)
|
|
|
|
|
|
|
|
|
|
|
|
- (UIImage *)normalizedImage {
|
|
|
|
- (UIImage *)normalizedImage {
|
|
|
|
if (self.imageOrientation == UIImageOrientationUp)
|
|
|
|
if (self.imageOrientation == UIImageOrientationUp) {
|
|
|
|
return self;
|
|
|
|
return self;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
|
|
|
|
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
|
|
|
|
[self drawInRect:(CGRect){{0, 0}, self.size}];
|
|
|
|
[self drawInRect:(CGRect){{0, 0}, self.size}];
|
|
|
@ -21,7 +18,6 @@
|
|
|
|
return normalizedImage;
|
|
|
|
return normalizedImage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- (UIImage *)resizedWithQuality:(CGInterpolationQuality)quality rate:(CGFloat)rate {
|
|
|
|
- (UIImage *)resizedWithQuality:(CGInterpolationQuality)quality rate:(CGFloat)rate {
|
|
|
|
UIImage *resized = nil;
|
|
|
|
UIImage *resized = nil;
|
|
|
|
CGFloat width = self.size.width * rate;
|
|
|
|
CGFloat width = self.size.width * rate;
|
|
|
@ -132,49 +128,42 @@
|
|
|
|
return resizedImage;
|
|
|
|
return resizedImage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (UIImage *)resizedImageToFitInSize:(CGSize)boundingSize scaleIfSmaller:(BOOL)scale {
|
|
|
|
- (UIImage *)resizedImageToFillPixelSize:(CGSize)dstSize
|
|
|
|
// get the image size (independant of imageOrientation)
|
|
|
|
{
|
|
|
|
CGImageRef imgRef = self.CGImage;
|
|
|
|
OWSAssert(dstSize.width > 0);
|
|
|
|
CGSize srcSize =
|
|
|
|
OWSAssert(dstSize.height > 0);
|
|
|
|
CGSizeMake(CGImageGetWidth(imgRef),
|
|
|
|
|
|
|
|
CGImageGetHeight(imgRef)); // not equivalent to self.size (which depends on the imageOrientation)!
|
|
|
|
UIImage *normalized = [self normalizedImage];
|
|
|
|
|
|
|
|
|
|
|
|
// adjust boundingSize to make it independant on imageOrientation too for farther computations
|
|
|
|
// Get the size in pixels, not points.
|
|
|
|
UIImageOrientation orient = self.imageOrientation;
|
|
|
|
CGSize srcSize = CGSizeMake(CGImageGetWidth(normalized.CGImage), CGImageGetHeight(normalized.CGImage));
|
|
|
|
switch (orient) {
|
|
|
|
OWSAssert(srcSize.width > 0);
|
|
|
|
case UIImageOrientationLeft:
|
|
|
|
OWSAssert(srcSize.height > 0);
|
|
|
|
case UIImageOrientationRight:
|
|
|
|
|
|
|
|
case UIImageOrientationLeftMirrored:
|
|
|
|
CGFloat widthRatio = srcSize.width / dstSize.width;
|
|
|
|
case UIImageOrientationRightMirrored:
|
|
|
|
CGFloat heightRatio = srcSize.height / dstSize.height;
|
|
|
|
boundingSize = CGSizeMake(boundingSize.height, boundingSize.width);
|
|
|
|
CGRect drawRect = CGRectZero;
|
|
|
|
break;
|
|
|
|
if (widthRatio > heightRatio) {
|
|
|
|
default:
|
|
|
|
drawRect.origin.y = 0;
|
|
|
|
// NOP
|
|
|
|
drawRect.size.height = dstSize.height;
|
|
|
|
break;
|
|
|
|
drawRect.size.width = dstSize.height * srcSize.width / srcSize.height;
|
|
|
|
}
|
|
|
|
OWSAssert(drawRect.size.width > dstSize.width);
|
|
|
|
|
|
|
|
drawRect.origin.x = (drawRect.size.width - dstSize.width) * -0.5f;
|
|
|
|
// Compute the target CGRect in order to keep aspect-ratio
|
|
|
|
|
|
|
|
CGSize dstSize;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!scale && (srcSize.width < boundingSize.width) && (srcSize.height < boundingSize.height)) {
|
|
|
|
|
|
|
|
// NSLog(@"Image is smaller, and we asked not to scale it in this case (scaleIfSmaller:NO)");
|
|
|
|
|
|
|
|
dstSize = srcSize; // no resize (we could directly return 'self' here, but we draw the image anyway to take
|
|
|
|
|
|
|
|
// image orientation into account)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
CGFloat wRatio = boundingSize.width / srcSize.width;
|
|
|
|
drawRect.origin.x = 0;
|
|
|
|
CGFloat hRatio = boundingSize.height / srcSize.height;
|
|
|
|
drawRect.size.width = dstSize.width;
|
|
|
|
|
|
|
|
drawRect.size.height = dstSize.width * srcSize.height / srcSize.width;
|
|
|
|
if (wRatio < hRatio) {
|
|
|
|
OWSAssert(drawRect.size.height > dstSize.height);
|
|
|
|
// NSLog(@"Width imposed, Height scaled ; ratio = %f",wRatio);
|
|
|
|
drawRect.origin.y = (drawRect.size.height - dstSize.height) * -0.5f;
|
|
|
|
dstSize = CGSizeMake(boundingSize.width, (CGFloat)floor(srcSize.height * wRatio));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// NSLog(@"Height imposed, Width scaled ; ratio = %f",hRatio);
|
|
|
|
|
|
|
|
dstSize = CGSizeMake((CGFloat)floor(srcSize.width * hRatio), boundingSize.height);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return [self resizedImageToSize:dstSize];
|
|
|
|
UIGraphicsBeginImageContextWithOptions(dstSize, NO, 1.f);
|
|
|
|
|
|
|
|
CGContextRef context = UIGraphicsGetCurrentContext();
|
|
|
|
|
|
|
|
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
|
|
|
|
|
|
|
|
[self drawInRect:drawRect];
|
|
|
|
|
|
|
|
UIImage *dstImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
|
|
|
|
|
|
UIGraphicsEndImageContext();
|
|
|
|
|
|
|
|
return dstImage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
@end
|
|
|
|