Skip to content

Instantly share code, notes, and snippets.

@anthonyvitocuaderno
Created June 5, 2018 09:01
Show Gist options
  • Save anthonyvitocuaderno/3e2f6f3281c040fbbef18d8ce806afb8 to your computer and use it in GitHub Desktop.
Save anthonyvitocuaderno/3e2f6f3281c040fbbef18d8ce806afb8 to your computer and use it in GitHub Desktop.
iOS Swift 3.3 UIImage extension
//
// UIImage.swift
// R2S
//
// Created by Vito Cuaderno on 3/22/18.
// Copyright © 2018 Total Integrated Resources. All rights reserved.
//
import UIKit
extension UIImage {
func mask(with color: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
defer { UIGraphicsEndImageContext() }
guard let context = UIGraphicsGetCurrentContext() else { return self }
context.translateBy(x: 0, y: self.size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.setBlendMode(.normal)
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
guard let mask = self.cgImage else { return self }
context.clip(to: rect, mask: mask)
color.setFill()
context.fill(rect)
guard let newImage = UIGraphicsGetImageFromCurrentImageContext() else { return self }
return newImage
}
func resize(to 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 = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
self.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
func toBase64() -> String {
if let jpegDatacropped = UIImageJPEGRepresentation(self, 80) {
let strBase64 = jpegDatacropped.base64EncodedString(options: .endLineWithLineFeed)
return strBase64
}
printE("UIImage failed to convert uiimage to base64")
return ""
}
func crop(withWidth width: Double, andHeight height: Double) -> UIImage? {
if let cgImage = self.cgImage {
let contextImage: UIImage = UIImage(cgImage: cgImage)
let contextSize: CGSize = contextImage.size
var posX: CGFloat = 0.0
var posY: CGFloat = 0.0
var cgwidth: CGFloat = CGFloat(width)
var cgheight: CGFloat = CGFloat(height)
// See what size is longer and create the center off of that
if contextSize.width > contextSize.height {
posX = ((contextSize.width - contextSize.height) / 2)
posY = 0
cgwidth = contextSize.height
cgheight = contextSize.height
} else {
posX = 0
posY = ((contextSize.height - contextSize.width) / 2)
cgwidth = contextSize.width
cgheight = contextSize.width
}
let rect: CGRect = CGRect(x: posX, y: posY, width: cgwidth, height: cgheight)
// Create bitmap image from context using the rect
var croppedContextImage: CGImage? = nil
if let contextImage = contextImage.cgImage {
if let croppedImage = contextImage.cropping(to: rect) {
croppedContextImage = croppedImage
}
}
// Create a new image based on the imageRef and rotate back to the original orientation
if let croppedImage:CGImage = croppedContextImage {
let image: UIImage = UIImage(cgImage: croppedImage, scale: self.scale, orientation: self.imageOrientation)
return image
}
}
printE("UIImage failed to crop")
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment