Created
February 17, 2017 17:54
-
-
Save danielt1263/b301ec03b223e0bfd80274769721ea35 to your computer and use it in GitHub Desktop.
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 characters
// | |
// UIImage+Extensions.swift | |
// | |
// Created by Daniel Tartaglia on 4/25/16. | |
// Copyright © Daniel Tartaglia. MIT License. | |
// | |
import UIKit | |
extension UIImage { | |
func imageScaledToSize(newSize: CGSize, scale: CGFloat) -> UIImage { | |
UIGraphicsBeginImageContextWithOptions(newSize, false, scale) | |
self.drawInRect(CGRect(origin: CGPoint(x: 0, y: 0), size: newSize)) | |
let newImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return newImage | |
} | |
func imageCroppedToSize(newSize: CGSize, scale: CGFloat) -> UIImage { | |
let horzOffset = (self.size.width - newSize.width) / 2.0 | |
let vertOffset = (self.size.height - newSize.height) / 2.0 | |
UIGraphicsBeginImageContextWithOptions(newSize, false, scale) | |
self.drawAtPoint(CGPoint(x: -horzOffset, y: -vertOffset), blendMode: .Copy, alpha: 1.0) | |
let result = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return result | |
} | |
func imageAspectFitted(newSize: CGSize, scale: CGFloat) -> UIImage { | |
let imageScaleFactor = max(newSize.width / self.size.width, newSize.height / self.size.height) | |
let scaledImage = self.imageScaledToSize(CGSize(width: self.size.width * imageScaleFactor, height: self.size.height * imageScaleFactor), scale: scale) | |
return scaledImage.imageCroppedToSize(newSize, scale: scale) | |
} | |
func squareImage() -> UIImage { | |
let size = min(self.size.width, self.size.height) | |
return self.imageCroppedToSize(CGSize(width: size, height: size), scale: 0) | |
} | |
func imageRotatedByDegrees(degrees: CGFloat, flip: Bool) -> UIImage { | |
let degreesToRadians: (CGFloat) -> CGFloat = { | |
return $0 / 180.0 * CGFloat(M_PI) | |
} | |
// calculate the size of the rotated view's containing box for our drawing space | |
let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero, size: size)) | |
let t = CGAffineTransformMakeRotation(degreesToRadians(degrees)); | |
rotatedViewBox.transform = t | |
let rotatedSize = rotatedViewBox.frame.size | |
// Create the bitmap context | |
UIGraphicsBeginImageContext(rotatedSize) | |
let bitmap = UIGraphicsGetCurrentContext() | |
// Move the origin to the middle of the image so we will rotate and scale around the center. | |
CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0, rotatedSize.height / 2.0); | |
// // Rotate the image context | |
CGContextRotateCTM(bitmap, degreesToRadians(degrees)); | |
// Now, draw the rotated/scaled image into the context | |
var yFlip: CGFloat | |
if(flip){ | |
yFlip = CGFloat(-1.0) | |
} else { | |
yFlip = CGFloat(1.0) | |
} | |
CGContextScaleCTM(bitmap, yFlip, -1.0) | |
CGContextDrawImage(bitmap, CGRectMake(-size.width / 2, -size.height / 2, size.width, size.height), CGImage) | |
let newImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return newImage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment