arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-camera/src/ios/UIImage+CropScaleOrientation.m
plugins/cordova-plugin-camera/src/ios/UIImage+CropScaleOrientation.mRaw
  1/*
  2 Licensed to the Apache Software Foundation (ASF) under one
  3 or more contributor license agreements.  See the NOTICE file
  4 distributed with this work for additional information
  5 regarding copyright ownership.  The ASF licenses this file
  6 to you under the Apache License, Version 2.0 (the
  7 "License"); you may not use this file except in compliance
  8 with the License.  You may obtain a copy of the License at
  9 
 10 http://www.apache.org/licenses/LICENSE-2.0
 11 
 12 Unless required by applicable law or agreed to in writing,
 13 software distributed under the License is distributed on an
 14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15 KIND, either express or implied.  See the License for the
 16 specific language governing permissions and limitations
 17 under the License.
 18 */
 19
 20#import "UIImage+CropScaleOrientation.h"
 21
 22@implementation UIImage (CropScaleOrientation)
 23
 24- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
 25{
 26    UIImage* sourceImage = self;
 27    UIImage* newImage = nil;
 28    CGSize imageSize = sourceImage.size;
 29    CGFloat width = imageSize.width;
 30    CGFloat height = imageSize.height;
 31    CGFloat targetWidth = targetSize.width;
 32    CGFloat targetHeight = targetSize.height;
 33    CGFloat scaleFactor = 0.0;
 34    CGFloat scaledWidth = targetWidth;
 35    CGFloat scaledHeight = targetHeight;
 36    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
 37    
 38    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
 39        CGFloat widthFactor = targetWidth / width;
 40        CGFloat heightFactor = targetHeight / height;
 41        
 42        if (widthFactor > heightFactor) {
 43            scaleFactor = widthFactor; // scale to fit height
 44        } else {
 45            scaleFactor = heightFactor; // scale to fit width
 46        }
 47        scaledWidth = width * scaleFactor;
 48        scaledHeight = height * scaleFactor;
 49        
 50        // center the image
 51        if (widthFactor > heightFactor) {
 52            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
 53        } else if (widthFactor < heightFactor) {
 54            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
 55        }
 56    }
 57    
 58    UIGraphicsBeginImageContext(targetSize); // this will crop
 59    
 60    CGRect thumbnailRect = CGRectZero;
 61    thumbnailRect.origin = thumbnailPoint;
 62    thumbnailRect.size.width = scaledWidth;
 63    thumbnailRect.size.height = scaledHeight;
 64    
 65    [sourceImage drawInRect:thumbnailRect];
 66    
 67    newImage = UIGraphicsGetImageFromCurrentImageContext();
 68    if (newImage == nil) {
 69        NSLog(@"could not scale image");
 70    }
 71    
 72    // pop the context to get back to the default
 73    UIGraphicsEndImageContext();
 74    return newImage;
 75}
 76
 77- (UIImage*)imageCorrectedForCaptureOrientation:(UIImageOrientation)imageOrientation
 78{
 79    float rotation_radians = 0;
 80    bool perpendicular = false;
 81    
 82    switch (imageOrientation) {
 83        case UIImageOrientationUp :
 84            rotation_radians = 0.0;
 85            break;
 86            
 87        case UIImageOrientationDown:
 88            rotation_radians = M_PI; // don't be scared of radians, if you're reading this, you're good at math
 89            break;
 90            
 91        case UIImageOrientationRight:
 92            rotation_radians = M_PI_2;
 93            perpendicular = true;
 94            break;
 95            
 96        case UIImageOrientationLeft:
 97            rotation_radians = -M_PI_2;
 98            perpendicular = true;
 99            break;
100            
101        default:
102            break;
103    }
104    
105    UIGraphicsBeginImageContext(CGSizeMake(self.size.width, self.size.height));
106    CGContextRef context = UIGraphicsGetCurrentContext();
107    
108    // Rotate around the center point
109    CGContextTranslateCTM(context, self.size.width / 2, self.size.height / 2);
110    CGContextRotateCTM(context, rotation_radians);
111    
112    CGContextScaleCTM(context, 1.0, -1.0);
113    float width = perpendicular ? self.size.height : self.size.width;
114    float height = perpendicular ? self.size.width : self.size.height;
115    CGContextDrawImage(context, CGRectMake(-width / 2, -height / 2, width, height), [self CGImage]);
116    
117    // Move the origin back since the rotation might've change it (if its 90 degrees)
118    if (perpendicular) {
119        CGContextTranslateCTM(context, -self.size.height / 2, -self.size.width / 2);
120    }
121    
122    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
123    UIGraphicsEndImageContext();
124    return newImage;
125}
126
127- (UIImage*)imageCorrectedForCaptureOrientation
128{
129    return [self imageCorrectedForCaptureOrientation:[self imageOrientation]];
130}
131
132- (UIImage*)imageByScalingNotCroppingForSize:(CGSize)targetSize
133{
134    UIImage* sourceImage = self;
135    UIImage* newImage = nil;
136    CGSize imageSize = sourceImage.size;
137    CGFloat width = imageSize.width;
138    CGFloat height = imageSize.height;
139    CGFloat targetWidth = targetSize.width;
140    CGFloat targetHeight = targetSize.height;
141    CGFloat scaleFactor = 0.0;
142    CGSize scaledSize = targetSize;
143    
144    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
145        CGFloat widthFactor = targetWidth / width;
146        CGFloat heightFactor = targetHeight / height;
147        
148        // opposite comparison to imageByScalingAndCroppingForSize in order to contain the image within the given bounds
149        if (widthFactor > heightFactor) {
150            scaleFactor = heightFactor; // scale to fit height
151        } else {
152            scaleFactor = widthFactor; // scale to fit width
153        }
154        scaledSize = CGSizeMake(MIN(width * scaleFactor, targetWidth), MIN(height * scaleFactor, targetHeight));
155    }
156    
157    // If the pixels are floats, it causes a white line in iOS8 and probably other versions too
158    scaledSize.width = (int)scaledSize.width;
159    scaledSize.height = (int)scaledSize.height;
160    
161    UIGraphicsBeginImageContext(scaledSize); // this will resize
162    
163    [sourceImage drawInRect:CGRectMake(0, 0, scaledSize.width, scaledSize.height)];
164    
165    newImage = UIGraphicsGetImageFromCurrentImageContext();
166    if (newImage == nil) {
167        NSLog(@"could not scale image");
168    }
169    
170    // pop the context to get back to the default
171    UIGraphicsEndImageContext();
172    return newImage;
173}
174
175@end