Instantly share code, notes, and snippets.

Embed
What would you like to do?
Swift Image Resizer
//
// RBResizer.swift
// Locker
//
// Created by Hampton Catlin on 6/20/14.
// Copyright (c) 2014 rarebit. All rights reserved.
//
import UIKit
func RBSquareImageTo(image: UIImage, size: CGSize) -> UIImage {
return RBResizeImage(RBSquareImage(image), size)
}
func RBSquareImage(image: UIImage) -> UIImage {
var originalWidth = image.size.width
var originalHeight = image.size.height
var edge: CGFloat
if originalWidth > originalHeight {
edge = originalHeight
} else {
edge = originalWidth
}
var posX = (originalWidth - edge) / 2.0
var posY = (originalHeight - edge) / 2.0
var cropSquare = CGRectMake(posX, posY, edge, edge)
var imageRef = CGImageCreateWithImageInRect(image.CGImage, cropSquare);
return UIImage(CGImage: imageRef, scale: UIScreen.mainScreen().scale, orientation: image.imageOrientation)
}
func RBResizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size
let widthRatio = targetSize.width / image.size.width
let heightRatio = targetSize.height / image.size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio)
} else {
newSize = CGSizeMake(size.width * widthRatio, size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRectMake(0, 0, newSize.width, newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.drawInRect(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
@appshish

This comment has been minimized.

appshish commented Jul 8, 2014

Awesome, very useful contribution.

@hkywal

This comment has been minimized.

hkywal commented Sep 30, 2014

Simple and fantastic! Thank you very much!

@westerlund

This comment has been minimized.

westerlund commented Dec 21, 2014

nice gist! I've updated it to work with xcode 6.1.1.
https://gist.github.com/westerlund/9e357a50315a7d9fe419

@Jan0707

This comment has been minimized.

Jan0707 commented Jan 9, 2015

Great work !

@vladimirzivanov

This comment has been minimized.

vladimirzivanov commented Jan 19, 2015

I don't know why it doesn't work for me. Example: my origin image size is 480x640, so cropSquare is 480x480, but instead of getting same size for imageRef, I'm getting 480x400, and then function is returning "img" with size 200x240 (?!). But if I use 0 instead of posY in CGRectMake (posX, 0, edge, edge) everything is fine (returning image size is 480x480), but the image is not centered but is cropped from the top, of course. Can someone help me?

@bashome

This comment has been minimized.

bashome commented Jan 26, 2015

I had the same problem as Vlad - after some research I think it has to do with the image orientation always being portrait in the CGImage ... I edited the code in the RBSquareImage function to remove the "edge" variable usage as well as the "posX" and "posY" variables and then changed the "cropSquare" calculation to this "var cropSquare = CGRectMake((originalHeight - originalWidth)/2, 0.0, originalWidth, originalWidth)" and then it worked in both portrait and landscape configuration and that function would return me a square image cropped in the middle. Cheers and I hope this helps.

@sieder

This comment has been minimized.

sieder commented Feb 20, 2015

This is so useful! thank you!

@willsteinmetz

This comment has been minimized.

willsteinmetz commented Mar 25, 2015

This is super helpful and saved me a lot of time. Thanks for sharing!

@lephukhanhhuy

This comment has been minimized.

lephukhanhhuy commented Apr 27, 2015

You should change this:
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)

to this:
UIGraphicsBeginImageContextWithOptions(newSize, false, UIScreen.mainScreen().scale)

for the best quality on iPhone 6+

@AchSum

This comment has been minimized.

AchSum commented May 27, 2015

Thank you ! :)

@RuliLG

This comment has been minimized.

RuliLG commented Aug 6, 2015

Awesome! Thank you! 😄

@kourindouhime

This comment has been minimized.

kourindouhime commented Aug 14, 2015

Thanks to the original author and thanks to @bashome for the fix. Really works now.

@iroller

This comment has been minimized.

iroller commented Sep 6, 2015

@Jaime5366

This comment has been minimized.

Jaime5366 commented Oct 6, 2015

thanks for you help me!

@tcerven

This comment has been minimized.

tcerven commented Nov 3, 2015

Thanks for posting this. Don't know why UIKit doesn't have a method for this.

@justinlevi

This comment has been minimized.

justinlevi commented Nov 30, 2015

Updated for use as a UIImage Extension
https://gist.github.com/justinlevi/ee037a4bb63598f6e56f

@FredRuniT

This comment has been minimized.

FredRuniT commented Jun 25, 2016

How do I implement this into my app?

@billy84

This comment has been minimized.

billy84 commented Sep 13, 2016

Very useful! Awesome!

@grafikri

This comment has been minimized.

grafikri commented Apr 13, 2018

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment