123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- //
- // 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
|