-
-
Save justinlevi/ee037a4bb63598f6e56f to your computer and use it in GitHub Desktop.
Revisions
-
justinlevi revised this gist
Nov 30, 2015 . 2 changed files with 62 additions and 59 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 @@ -1,59 +0,0 @@ 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,62 @@ // // RBResizer.swift // Locker // // Created by Hampton Catlin on 6/20/14. // Copyright (c) 2014 rarebit. All rights reserved. // Updated by Justin Winter on 11/29/15. // extension UIImage { func RBSquareImageTo(size: CGSize) -> UIImage? { return self.RBSquareImage()?.RBResizeImage(size) } func RBSquareImage() -> UIImage? { let originalWidth = self.size.width let originalHeight = self.size.height var edge: CGFloat if originalWidth > originalHeight { edge = originalHeight } else { edge = originalWidth } let posX = (originalWidth - edge) / 2.0 let posY = (originalHeight - edge) / 2.0 let cropSquare = CGRectMake(posX, posY, edge, edge) let imageRef = CGImageCreateWithImageInRect(self.CGImage, cropSquare); return UIImage(CGImage: imageRef!, scale: UIScreen.mainScreen().scale, orientation: self.imageOrientation) } func RBResizeImage(targetSize: CGSize) -> UIImage { let size = self.size let widthRatio = targetSize.width / self.size.width let heightRatio = targetSize.height / self.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, UIScreen.mainScreen().scale) self.drawInRect(rect) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } } -
HamptonMakes created this gist
Jun 27, 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,59 @@ // // 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 }