Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extension CGSize {
func scaled(byFactor: CGFloat) -> CGSize {
return CGSize(width: width*byFactor, height: height*byFactor)
}
}
extension UIImage {
static func drawnVia(size: CGSize, opaque: Bool = false, _ scale: CGFloat = 0.0, draw: () -> ()) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, opaque, scale)
draw()
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result!
}
}
func scaleAndCropImage(image: UIImage, toSize size: CGSize, fitImage: Bool = true) -> UIImage {
guard image.size != size else { return image }
// Calculate scale factor for fit or fill
let (widthFactor, heightFactor) = (size.width / image.size.width, size.height / image.size.height)
let scaleFactor = fitImage ? min(widthFactor, heightFactor) : max(widthFactor, heightFactor)
// Establish drawing destination, which may start outside the drawing context bounds
let scaledSize = image.size.scaled(scaleFactor)
let drawingOrigin = CGPoint(x: (size.width - scaledSize.width) / 2.0, y: (size.height - scaledSize.height) / 2.0)
// Perform drawing and return image
return UIImage.drawnVia(size) {
UIColor.blackColor().setFill(); UIRectFill(CGRect(origin: .zero, size: size))
image.drawInRect(CGRect(origin: drawingOrigin, size: scaledSize))
}
}
@erica

This comment has been minimized.

Copy link

commented May 31, 2016

Okay, fine:

func * (lhs: CGSize, rhs: CGFloat) -> CGSize { return CGSize(width: lhs.width * rhs, height: lhs.height * rhs) }

extension UIImage {
    func scale(to destSize: CGSize, fitImage: Bool = true, backgroundColor: UIColor = .blackColor()) -> UIImage {
        guard size != destSize else { return self }

        // Calculate scale factor for fit or fill
        let scaleFactor = (fitImage ? min : max)(destSize.width / size.width, destSize.height / size.height)

        // Establish drawing destination, which may start outside the drawing context bounds
        let scaledSize = size * scaleFactor
        let drawingOrigin = CGPoint(
            x: (destSize.width - scaledSize.width) / 2.0,
            y: (destSize.height - scaledSize.height) / 2.0)
        let drawingRect = CGRect(origin: drawingOrigin, size: scaledSize)

        // Perform drawing and return image
        UIGraphicsBeginImageContextWithOptions(destSize, false, 0.0); defer { UIGraphicsEndImageContext() }
        backgroundColor.setFill(); UIRectFill(CGRect(origin: .zero, size: destSize))
        self.drawInRect(drawingRect)
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}
@erica

This comment has been minimized.

Copy link

commented May 31, 2016

And:

func * (lhs: CGSize, rhs: CGFloat) -> CGSize { return CGSize(width: lhs.width * rhs, height: lhs.height * rhs) }

extension UIImage {
    func scale(to destSize: CGSize, fitImage: Bool = true, backgroundColor: UIColor = .blackColor()) -> UIImage {
        guard size != destSize else { return self }

        // Establish drawing destination, which may start outside the drawing context bounds
        let scaleFactor = (fitImage ? min : max)(destSize.width / size.width, destSize.height / size.height)
        let scaledSize = size * scaleFactor
        let drawingRect = CGRect(origin: .zero, size: scaledSize)
            .offsetBy(dx: (destSize.width - scaledSize.width) / 2.0,
                      dy: (destSize.height - scaledSize.height) / 2.0)

        // Perform drawing and return image
        UIGraphicsBeginImageContextWithOptions(destSize, false, 0.0); defer { UIGraphicsEndImageContext() }
        backgroundColor.setFill(); UIRectFill(CGRect(origin: .zero, size: destSize))
        self.drawInRect(drawingRect)
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.