abbaspour-narjes
9/6/2017 - 6:16 AM

Fit UIImag In UIImageview

Fit UIImag In UIImageview

extension UIImageView{
    func fitme( _layout:CGFloat,_data:Data)-> CGFloat?{
        let a=UIImage(data: _data)
        self.image = a
        self.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        let bounds = UIScreen.main.bounds
        let width = bounds.size.width
        
        var b=a?.size.width
        
        
        if (b)! > width{ //////////biger
            let c = b! - width
            
            b=(c*100)/b! //percent
            let h=a?.size.height
            let height=(h!*b!)/100
           return h! - height
        }
        b=a?.size.width
        
        if (b)! < width{ ///////smaller
            let c = width - b!
            b=(c*100)/width //percent
            let h=a?.size.height
            let height=(h!*b!)/100
            return h! + height
        }
        if (b)! == width{
            return (a?.size.height)!
        }
        
        
        self.contentMode = .scaleToFill
        
        self.clipsToBounds = true
        return nil
    
    }