// // UIImage+CropRotate.m // // Copyright 2015 Timothy Oliver. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #import "UIImage+CropRotate.h" @implementation UIImage (CropRotate) - (UIImage *)croppedImageWithFrame:(CGRect)frame angle:(NSInteger)angle { UIImage *croppedImage = nil; CGPoint drawPoint = CGPointZero; UIGraphicsBeginImageContextWithOptions(frame.size, YES, self.scale); { CGContextRef context = UIGraphicsGetCurrentContext(); //To conserve memory in not needing to completely re-render the image re-rotated, //map the image to a view and then use Core Animation to manipulate its rotation if (angle != 0) { UIImageView *imageView = [[UIImageView alloc] initWithImage:self]; imageView.layer.minificationFilter = @"nearest"; imageView.layer.magnificationFilter = @"neareset"; imageView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, angle * (M_PI/180.0f)); CGRect rotatedRect = CGRectApplyAffineTransform(imageView.bounds, imageView.transform); UIView *containerView = [[UIView alloc] initWithFrame:(CGRect){CGPointZero, rotatedRect.size}]; [containerView addSubview:imageView]; imageView.center = containerView.center; CGContextTranslateCTM(context, -frame.origin.x, -frame.origin.y); [containerView.layer renderInContext:context]; } else { CGContextTranslateCTM(context, -frame.origin.x, -frame.origin.y); [self drawAtPoint:drawPoint]; } croppedImage = UIGraphicsGetImageFromCurrentImageContext(); } UIGraphicsEndImageContext(); return croppedImage; } @end