图片压缩处理

来源:互联网 发布:双四选一数据选择器 编辑:程序博客网 时间:2024/06/06 13:23
import Foundationimport UIKitextension UIImage {    //compress image to quality(0,1)    class func compressImageToQuality(image:UIImage, quality:CGFloat) -> UIImage {        if quality >= 1 || quality <= 0 {            return image        }        //compress image data        let imageData = UIImageJPEGRepresentation(image, quality)        if let data = imageData {            return UIImage(data: data)!        }        return image    }    //compress image to scale size    class func scaleImageToSize(image:UIImage, size:CGSize) -> UIImage {        let width = size.width        let height = size.height        if width == 0 || height == 0 {            return image        }        //Create a graphics image context with new size        UIGraphicsBeginImageContext(size)        //draw scale image in rect        image.drawInRect(CGRect(x: 0, y: 0, width: width, height: height))        //get the scale image from the context        let scaleImage = UIGraphicsGetImageFromCurrentImageContext()        //remove the current context from the top of the stack        UIGraphicsEndImageContext()        return scaleImage    }}
0 0
原创粉丝点击