// // UIImage+Resize.m // NYXImagesKit // // Created by @Nyx0uf on 02/05/11. // Copyright 2012 Nyx0uf. All rights reserved. // www.cocoaintheshell.com // #import "UIImage+Resizing.h" @implementation UIImage (NYX_Resizing) -(UIImage*)cropToSize:(CGSize)newSize usingMode:(NYXCropMode)cropMode { const CGSize size = self.size; CGFloat x, y; switch (cropMode) { case NYXCropModeTopLeft: x = y = 0.0f; break; case NYXCropModeTopCenter: x = (size.width - newSize.width) * 0.5f; y = 0.0f; break; case NYXCropModeTopRight: x = size.width - newSize.width; y = 0.0f; break; case NYXCropModeBottomLeft: x = 0.0f; y = size.height - newSize.height; break; case NYXCropModeBottomCenter: x = newSize.width * 0.5f; y = size.height - newSize.height; break; case NYXCropModeBottomRight: x = size.width - newSize.width; y = size.height - newSize.height; break; case NYXCropModeLeftCenter: x = 0.0f; y = (size.height - newSize.height) * 0.5f; break; case NYXCropModeRightCenter: x = size.width - newSize.width; y = (size.height - newSize.height) * 0.5f; break; case NYXCropModeCenter: x = (size.width - newSize.width) * 0.5f; y = (size.height - newSize.height) * 0.5f; break; default: // Default to top left x = y = 0.0f; break; } CGRect cropRect = CGRectMake(x * self.scale, y * self.scale, newSize.width * self.scale, newSize.height * self.scale); /// Create the cropped image CGImageRef croppedImageRef = CGImageCreateWithImageInRect(self.CGImage, cropRect); UIImage* cropped = [UIImage imageWithCGImage:croppedImageRef scale:self.scale orientation:self.imageOrientation]; /// Cleanup CGImageRelease(croppedImageRef); return cropped; } /* Convenience method to crop the image from the top left corner */ -(UIImage*)cropToSize:(CGSize)newSize { return [self cropToSize:newSize usingMode:NYXCropModeTopLeft]; } -(UIImage*)scaleByFactor:(float)scaleFactor { CGSize scaledSize = CGSizeMake(self.size.width * scaleFactor, self.size.height * scaleFactor); return [self scaleToFillSize:scaledSize]; } -(UIImage*)scaleToFillSize:(CGSize)newSize { const size_t destWidth = (size_t)(newSize.width * self.scale); const size_t destHeight = (size_t)(newSize.height * self.scale); /// Create an ARGB bitmap context CGContextRef bmContext = NYXCreateARGBBitmapContext(destWidth, destHeight, destWidth * kNyxNumberOfComponentsPerARBGPixel); if (!bmContext) return nil; /// Image quality CGContextSetShouldAntialias(bmContext, true); CGContextSetAllowsAntialiasing(bmContext, true); CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh); /// Draw the image in the bitmap context UIGraphicsPushContext(bmContext); CGContextDrawImage(bmContext, CGRectMake(0.0f, 0.0f, destWidth, destHeight), self.CGImage); UIGraphicsPopContext(); /// Create an image object from the context CGImageRef scaledImageRef = CGBitmapContextCreateImage(bmContext); UIImage* scaled = [UIImage imageWithCGImage:scaledImageRef scale:self.scale orientation:self.imageOrientation]; /// Cleanup CGImageRelease(scaledImageRef); CGContextRelease(bmContext); return scaled; } -(UIImage*)scaleToFitSize:(CGSize)newSize { /// Keep aspect ratio size_t destWidth, destHeight; if (self.size.width > self.size.height) { destWidth = (size_t)newSize.width; destHeight = (size_t)(self.size.height * newSize.width / self.size.width); } else { destHeight = (size_t)newSize.height; destWidth = (size_t)(self.size.width * newSize.height / self.size.height); } if (destWidth > newSize.width) { destWidth = (size_t)newSize.width; destHeight = (size_t)(self.size.height * newSize.width / self.size.width); } if (destHeight > newSize.height) { destHeight = (size_t)newSize.height; destWidth = (size_t)(self.size.width * newSize.height / self.size.height); } return [self scaleToFillSize:CGSizeMake(destWidth, destHeight)]; } -(UIImage*)scaleToCoverSize:(CGSize)newSize { /// Keep aspect ratio size_t destWidth, destHeight; if (self.size.width > self.size.height) { destWidth = (size_t)newSize.width; destHeight = (size_t)(self.size.height * newSize.width / self.size.width); } else { destHeight = (size_t)newSize.height; destWidth = (size_t)(self.size.width * newSize.height / self.size.height); } return [self scaleToFillSize:CGSizeMake(destWidth, destHeight)]; } @end