Last active
February 1, 2024 19:04
-
-
Save tomasbasham/10533743 to your computer and use it in GitHub Desktop.
Revisions
-
tomasbasham revised this gist
Dec 23, 2014 . 1 changed file with 19 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,25 +10,25 @@ @implementation UIImage (scale) * @return a new scaled image. */ - (UIImage *)scaleImageToSize:(CGSize)newSize { CGRect scaledImageRect = CGRectZero; CGFloat aspectWidth = newSize.width / self.size.width; CGFloat aspectHeight = newSize.height / self.size.height; CGFloat aspectRatio = MIN ( aspectWidth, aspectHeight ); scaledImageRect.size.width = self.size.width * aspectRatio; scaledImageRect.size.height = self.size.height * aspectRatio; scaledImageRect.origin.x = (newSize.width - scaledImageRect.size.width) / 2.0f; scaledImageRect.origin.y = (newSize.height - scaledImageRect.size.height) / 2.0f; UIGraphicsBeginImageContextWithOptions( newSize, NO, 0 ); [self drawInRect:scaledImageRect]; UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; } @end -
tomasbasham created this gist
Apr 12, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ @implementation UIImage (scale) /** * Scales an image to fit within a bounds with a size governed by * the passed size. Also keeps the aspect ratio. * * Switch MIN to MAX for aspect fill instead of fit. * * @param newSize the size of the bounds the image must fit within. * @return a new scaled image. */ - (UIImage *)scaleImageToSize:(CGSize)newSize { CGRect scaledImageRect = CGRectZero; CGFloat aspectWidth = newSize.width / self.size.width; CGFloat aspectHeight = newSize.height / self.size.height; CGFloat aspectRatio = MIN ( aspectWidth, aspectHeight ); scaledImageRect.size.width = self.size.width * aspectRatio; scaledImageRect.size.height = self.size.height * aspectRatio; scaledImageRect.origin.x = (newSize.width - scaledImageRect.size.width) / 2.0f; scaledImageRect.origin.y = (newSize.height - scaledImageRect.size.height) / 2.0f; UIGraphicsBeginImageContextWithOptions( newSize, NO, 0 ); [self drawInRect:scaledImageRect]; UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; } @end