How to set image according to imageView in swift


Copy and pest that function to your project manager or main file

func imageAdjustAccordingSize(_ image: UIImage?, size cgSize: CGSize) -> UIImage? {
        let scale: CGFloat = max(cgSize.width / (image?.size.width ?? 0.0), cgSize.height / (image?.size.height ?? 0.0))
        let width: CGFloat = (image?.size.width ?? 0.0) * scale
        let height: CGFloat = (image?.size.height ?? 0.0) * scale
        let imageRect = CGRect(x: (cgSize.width - width) / 2.0, y: (cgSize.height - height) / 2.0, width: width, height: height)
        UIGraphicsBeginImageContextWithOptions(cgSize, false, 0)
        image?.draw(in: imageRect)
        let newImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }





Thank you Enjoy it.


Comments

Popular posts from this blog

Windows Keys

Important extensions while create a new App in swift

S.O.L.I.D. Principles with Example (Swift & Dart)