美文网首页
图片实现等比压缩

图片实现等比压缩

作者: XC1988 | 来源:发表于2018-08-09 09:26 被阅读0次
    let screenHeight:CGFloat = UIScreen.main.bounds.size.height
    let screenWidth:CGFloat = UIScreen.main.bounds.size.width
    func useImage(image: UIImage) -> NSData {
        //实现等比例缩放
        let hfactor = image.size.width / screenWidth
        let vfactor = image.size.height / screenHeight
        let factor = fmax(hfactor, vfactor)
        //画布大小
        let newWith: CGFloat = image.size.width / factor
        let newHeigth: CGFloat = image.size.height / factor
        let newSize = CGSize(width: newWith, height: newHeigth)
        UIGraphicsBeginImageContext(newSize)
        image.drawInRect(CGRect(x: 0, y: 0, width: newWith, height: newHeigth))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        //图像压缩
        let newImageData = UIImageJPEGRepresentation(newImage, 0.5)
        return newImageData!
    }

    相关文章

      网友评论

          本文标题:图片实现等比压缩

          本文链接:https://www.haomeiwen.com/subject/svqqbftx.html