import UIKit extension UIImage { /// Resizes an image to the specified size. /// /// - Parameters: /// - size: the size we desire to resize the image to. /// /// - Returns: the resized image. /// func imageWithSize(size: CGSize) -> UIImage? { UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale); let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height); draw(in: rect) let resultingImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resultingImage } /// Resizes an image to the specified size and adds an extra transparent margin at all sides of /// the image. /// /// - Parameters: /// - size: the size we desire to resize the image to. /// - extraMargin: the extra transparent margin to add to all sides of the image. /// /// - Returns: the resized image. The extra margin is added to the input image size. So that /// the final image's size will be equal to: /// `CGSize(width: size.width + extraMargin * 2, height: size.height + extraMargin * 2)` /// func imageWithSize(size: CGSize, extraMargin: CGFloat) -> UIImage? { let imageSize = CGSize(width: size.width + extraMargin * 2, height: size.height + extraMargin * 2) UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.main.scale); let drawingRect = CGRect(x: extraMargin, y: extraMargin, width: size.width, height: size.height) draw(in: drawingRect) let resultingImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resultingImage } /// Resizes an image to the specified size. /// /// - Parameters: /// - size: the size we desire to resize the image to. /// - roundedRadius: corner radius /// /// - Returns: the resized image with rounded corners. /// func imageWithSize(size: CGSize, roundedRadius radius: CGFloat) -> UIImage? { UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale) if let currentContext = UIGraphicsGetCurrentContext() { let rect = CGRect(origin: .zero, size: size) currentContext.addPath(UIBezierPath(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: radius, height: radius)).cgPath) currentContext.clip() //Don't use CGContextDrawImage, coordinate system origin in UIKit and Core Graphics are vertical oppsite. draw(in: rect) currentContext.drawPath(using: .fillStroke) let roundedCornerImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return roundedCornerImage } return nil } }