Untitled

mail@pastecode.io avatar
unknown
swift
2 years ago
3.7 kB
5
Indexable
Never
import UIKit

extension UIImage {
    func resizedToFit(size: CGSize) -> UIImage? {
        var boundingSize = size
        guard let cgImageRef = cgImage else {
            return nil
        }
        let srcSize = CGSize(width: cgImageRef.width, height: cgImageRef.height)
        
        switch(imageOrientation) {
        case .left, .right, .leftMirrored, .rightMirrored:
            boundingSize = CGSize(width: size.height, height: size.width)
        default:
            break
        }
        
        var dstSize: CGSize!
        
        if srcSize.width < boundingSize.width && srcSize.height < boundingSize.height {
            dstSize = srcSize
        } else {
            let wRatio = boundingSize.width / srcSize.width
            let hRatio = boundingSize.height / srcSize.height
            
            if wRatio < hRatio {
                dstSize = CGSize(width: boundingSize.width, height: floor(srcSize.height * wRatio))
            } else {
                dstSize = CGSize(width: floor(srcSize.width * hRatio), height: boundingSize.height)
            }
        }
        return resizeImage(to: dstSize)
    }
    
    func resizeImage(to size: CGSize) -> UIImage? {
        var dstSize = size
        guard let cgImageRef = cgImage else {
            return nil
        }
        let srcSize = CGSize(width: cgImageRef.width, height: cgImageRef.height)
        
        if srcSize.equalTo(size) {
            return self
        }
        
        let scaleRatio = dstSize.width / srcSize.width
        var transform = CGAffineTransform.identity
        
        switch(imageOrientation) {
        case .up:
            transform = .identity
        case .upMirrored:
            transform = CGAffineTransform(translationX: srcSize.width, y: 0).scaledBy(x: -1, y: 1)
        case .down:
            transform = CGAffineTransform(translationX: srcSize.width, y: srcSize.height).rotated(by: .pi)
        case .downMirrored:
            transform = CGAffineTransform(translationX: 0, y: srcSize.height).scaledBy(x: 1, y: -1)
        case .leftMirrored:
            dstSize = CGSize(width: size.height, height: size.width)
            transform = CGAffineTransform(translationX: srcSize.height, y: srcSize.width).scaledBy(x: -1, y: 1).rotated(by: 1.5 * .pi)
        case .left:
            dstSize = CGSize(width: size.height, height: size.width)
            transform = CGAffineTransform(translationX: 0, y: srcSize.width).rotated(by: 1.5 * .pi)
        case .rightMirrored:
            dstSize = CGSize(width: size.height, height: size.width)
            transform = transform.scaledBy(x: -1, y: 1).rotated(by: .pi / 2)
        case .right:
            dstSize = CGSize(width: size.height, height: size.width)
            transform = CGAffineTransform(translationX: srcSize.height, y: 0).rotated(by: .pi / 2)
        @unknown default:
            break
        }
        
        UIGraphicsBeginImageContextWithOptions(dstSize, false, scale)
        let context = UIGraphicsGetCurrentContext()
        
        if imageOrientation == .right || imageOrientation == .left {
            context?.scaleBy(x: -scaleRatio, y: scaleRatio)
            context?.translateBy(x: -srcSize.height, y: 0)
        } else {
            context?.scaleBy(x: scaleRatio, y: -scaleRatio)
            context?.translateBy(x: 0, y: -srcSize.height)
        }
        
        context?.concatenate(transform)
        context?.draw(cgImageRef, in: CGRect(x: 0, y: 0, width: srcSize.width, height: srcSize.height))
        
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return resizedImage
    }
}