Skip to content

Instantly share code, notes, and snippets.

@depthlove
Forked from tomasbasham/UIImage+Scale.m
Created December 19, 2017 05:22
Show Gist options
  • Save depthlove/689fe277a1ab6e16d11a66226d6063bd to your computer and use it in GitHub Desktop.
Save depthlove/689fe277a1ab6e16d11a66226d6063bd to your computer and use it in GitHub Desktop.

Revisions

  1. @tomasbasham tomasbasham revised this gist Dec 23, 2014. 1 changed file with 19 additions and 19 deletions.
    38 changes: 19 additions & 19 deletions UIImage+Scale.m
    Original 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;
    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
  2. @tomasbasham tomasbasham created this gist Apr 12, 2014.
    34 changes: 34 additions & 0 deletions UIImage+Scale.m
    Original 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