天天看点

Swift-Extension的理解

extension NSString{
    func printSelf() -> Void {
        print(self)
    }
}

extension Int{
    var FloatValue:Float {return Float(self)}
    var DoubleValue:Double {return Double(self)}
}

extension UIImage{
    var height:CGFloat{return self.size.height}
    var width:CGFloat{return self.size.width}
    
    func imageCompress(targetWidth:CGFloat) -> UIImage {
        let targetHeight = (targetWidth/width)*height
        UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight))
        self.drawInRect(CGRectMake(0, 0, targetWidth, targetHeight))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
        
    }
}