| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- //
- // UIImage+Extension.swift
- // MuchinfoSignUp
- //
- // Created by zhongyuan on 2017/4/11.
- // Copyright © 2018年 zhongyuan. All rights reserved.
- //
- import UIKit
- // MARK: - 图片拓展类
- extension UIImage {
-
- // MARK: - 降低质量
- /// 根据指定体积大小来压缩图片的方法
- ///
- /// - Parameter maxSize: 指定压缩大小
- /// - Returns: 压缩后的图片数据
- func resetSizeOfImageData(maxSize: Int) -> Data {
-
- //先判断当前质量是否满足要求,不满足再进行压缩
- var finallImageData = UIImage.jpegData(self)(compressionQuality: 1.0)
- let sizeOrigin = finallImageData?.count
- let sizeOriginKB = sizeOrigin! / 1024
- if sizeOriginKB <= maxSize {
- return finallImageData!
- }
-
- //先调整分辨率
- var defaultSize = CGSize(width: 1024, height: 1024)
- let newImage = self.newSizeImage(size: defaultSize, source_image: self)
-
- finallImageData = UIImage.jpegData(newImage)(compressionQuality: 1.0)
-
- //保存压缩系数
- let compressionQualityArr = NSMutableArray()
- let avg = CGFloat(1.0/250)
- var value = avg
-
- var i = 250
- repeat {
- i -= 1
- value = CGFloat(i)*avg
- compressionQualityArr.add(value)
- } while i >= 1
-
- /*
- 调整大小
- 说明:压缩系数数组compressionQualityArr是从大到小存储。
- */
- //思路:使用二分法搜索
- finallImageData = self.halfFuntion(arr: compressionQualityArr.copy() as! [CGFloat], image: newImage, sourceData: finallImageData!, maxSize: maxSize)
- //如果还是未能压缩到指定大小,则进行降分辨率
- while finallImageData?.count == 0 {
- //每次降100分辨率
- if defaultSize.width-100 <= 0 || defaultSize.height-100 <= 0 {
- break
- }
- defaultSize = CGSize(width: defaultSize.width-100, height: defaultSize.height-100)
-
- let image = self.newSizeImage(size: defaultSize, source_image: UIImage(data: UIImage.jpegData(newImage)(compressionQuality: compressionQualityArr.lastObject as! CGFloat)!)!)
- finallImageData = self.halfFuntion(arr: compressionQualityArr.copy() as! [CGFloat], image: image, sourceData: UIImage.jpegData(image)(compressionQuality: 1.0)!, maxSize: maxSize)
- }
-
- return finallImageData!
- }
-
- /// 根据指定的分辨率大小来等比率压缩图片的方法
- ///
- /// - Parameters:
- /// - size: 指定大小
- /// - source_image: 目标图片数据
- /// - Returns: 压缩后的图片数据
- func newSizeImage(size: CGSize, source_image: UIImage) -> UIImage {
- var newSize = CGSize(width: source_image.size.width, height: source_image.size.height)
- let tempHeight = newSize.height / size.height
- let tempWidth = newSize.width / size.width
-
- if tempWidth > 1.0 && tempWidth > tempHeight {
- newSize = CGSize(width: source_image.size.width / tempWidth, height: source_image.size.height / tempWidth)
- } else if tempHeight > 1.0 && tempWidth < tempHeight {
- newSize = CGSize(width: source_image.size.width / tempHeight, height: source_image.size.height / tempHeight)
- }
-
- UIGraphicsBeginImageContext(newSize)
- source_image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
- let newImage = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- return newImage!
- }
-
- // MARK: - 二分法
- func halfFuntion(arr: [CGFloat], image: UIImage, sourceData finallImageData: Data, maxSize: Int) -> Data? {
- var tempFinallImageData = finallImageData
-
- var tempData = Data.init()
- var start = 0
- var end = arr.count - 1
- var index = 0
-
- var difference = Int.max
- while start <= end {
- index = start + (end - start)/2
-
- tempFinallImageData = UIImage.jpegData(image)(compressionQuality: arr[index])!
- let sizeOrigin = tempFinallImageData.count
- let sizeOriginKB = sizeOrigin / 1024
-
- if sizeOriginKB > maxSize {
- start = index + 1
- } else if sizeOriginKB < maxSize {
- if maxSize-sizeOriginKB < difference {
- difference = maxSize-sizeOriginKB
- tempData = tempFinallImageData
- }
- end = index - 1
- } else {
- break
- }
- }
- return tempData
- }
-
- //图片压缩 1000kb以下的图片控制在100kb-200kb之间
- func compressImageSize() -> Data{
-
- var zipImageData = UIImage.jpegData(self)(compressionQuality: 1.0)!//UIImageJPEGRepresentation(self, 1.0)!
- let originalImgSize = zipImageData.count/1024 as Int //获取图片大小
-
- if originalImgSize>1500 {
- zipImageData = UIImage.jpegData(self)(compressionQuality: 0.2)!//UIImageJPEGRepresentation(self,0.2)!
- } else if originalImgSize>600 {
- zipImageData = UIImage.jpegData(self)(compressionQuality: 0.4)!//UIImageJPEGRepresentation(self,0.4)!
- } else if originalImgSize>400 {
- zipImageData = UIImage.jpegData(self)(compressionQuality: 0.6)!//UIImageJPEGRepresentation(self,0.6)!
- } else if originalImgSize>300 {
- zipImageData = UIImage.jpegData(self)(compressionQuality: 0.7)!//UIImageJPEGRepresentation(self,0.7)!
- } else if originalImgSize>200 {
- zipImageData = UIImage.jpegData(self)(compressionQuality: 0.8)!//UIImageJPEGRepresentation(self,0.8)!
- }
- return zipImageData
- }
-
- func resizeImage() -> UIImage{
-
- //prepare constants
- let width = self.size.width
- let height = self.size.height
- let scale = width/height
-
- var sizeChange = CGSize()
-
- if width <= 1280 && height <= 1280{ //a,图片宽或者高均小于或等于1280时图片尺寸保持不变,不改变图片大小
- return self
- } else if width > 1280 || height > 1280 {//b,宽或者高大于1280,但是图片宽度高度比小于或等于2,则将图片宽或者高取大的等比压缩至1280
-
- if scale <= 2 && scale >= 1 {
- let changedWidth:CGFloat = 1280
- let changedheight:CGFloat = changedWidth / scale
- sizeChange = CGSize(width: changedWidth, height: changedheight)
-
- } else if scale >= 0.5 && scale <= 1 {
-
- let changedheight:CGFloat = 1280
- let changedWidth:CGFloat = changedheight * scale
- sizeChange = CGSize(width: changedWidth, height: changedheight)
-
- } else if width > 1280 && height > 1280 {//宽以及高均大于1280,但是图片宽高比大于2时,则宽或者高取小的等比压缩至1280
-
- if scale > 2 {//高的值比较小
-
- let changedheight:CGFloat = 1280
- let changedWidth:CGFloat = changedheight * scale
- sizeChange = CGSize(width: changedWidth, height: changedheight)
-
- } else if scale < 0.5{//宽的值比较小
-
- let changedWidth:CGFloat = 1280
- let changedheight:CGFloat = changedWidth / scale
- sizeChange = CGSize(width: changedWidth, height: changedheight)
-
- }
- } else {//d, 宽或者高,只有一个大于1280,并且宽高比超过2,不改变图片大小
- return self
- }
- }
-
- UIGraphicsBeginImageContext(sizeChange)
-
- //draw resized image on Context
- self.draw(in: CGRect(origin: CGPoint.zero, size: CGSize(width: sizeChange.width, height: sizeChange.height)))
-
- //create UIImage
- let resizedImg = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
-
- return resizedImg!
-
- }
-
- /// 传入图片image回传对应的base64字符串,默认不带有data标识,
- func imageToBase64String() -> String? {
-
- /// 根据图片得到对应的二进制编码
- guard let imageData = self.pngData() else {
- return nil
- }
-
- /// 根据二进制编码得到对应的base64字符串
- var base64String = imageData.base64EncodedString(options: .endLineWithCarriageReturn)
- /// 根据格式拼接数据头 添加header信息,扩展名信息
- base64String = "data:image/png;base64," + base64String
- return base64String
- }
-
- /// 图片旋转
- /// - Parameter orientation: 角度
- func transRotation(_ orientation: UIImage.Orientation) -> UIImage? {
-
- var rotate: Double = 0.0
- var rect: CGRect = CGRect.zero
- var translateX: CGFloat = 0.0;
- var translateY: CGFloat = 0.0;
- var scaleX: CGFloat = 1.0;
- var scaleY: CGFloat = 1.0;
-
- switch orientation {
- case .left:
- rotate = .pi/2
- rect = CGRect(x: 0.0, y: 0.0, width: self.size.height, height: self.size.width)
- translateX = 0
- translateY = -rect.size.width
- scaleY = rect.size.width/rect.size.height
- scaleX = rect.size.height/rect.size.width
- case .right:
- rotate = 33 * .pi/2
- rect = CGRect(x: 0, y: 0, width: self.size.height, height:self.size.width)
- translateX = -rect.size.height
- translateY = 0
- scaleY = rect.size.width/rect.size.height
- scaleX = rect.size.height/rect.size.width
- case .down:
- rotate = .pi
- rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
- translateX = -rect.size.width
- translateY = -rect.size.height
- default:
- rotate = 0.0
- rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
- translateX = 0
- translateY = 0
- }
-
- UIGraphicsBeginImageContext(rect.size);
- let context = UIGraphicsGetCurrentContext()
-
- /// 做CTM变换
- context!.translateBy(x: 0.0, y: rect.size.height)
- context!.scaleBy(x: 1.0, y: -1.0)
- context!.rotate(by: CGFloat(rotate))
- context!.translateBy(x: translateX, y: translateY)
- context!.scaleBy(x: scaleX, y: scaleY)
-
- /// 绘制图片
- context?.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: rect.size.width, height: rect.size.height))
- return UIGraphicsGetImageFromCurrentImageContext()
- }
-
- /// 按照尺寸大小压缩图片
- /// - Parameter size: size
- func reSizeImage(_ size: CGSize)-> UIImage {
- UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale);
- self.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height));
- let image: UIImage = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage();
- UIGraphicsEndImageContext();
- return image;
- }
-
- static func image(_ color: UIColor, _ size: CGSize) -> UIImage {
- let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
- UIGraphicsBeginImageContext(rect.size )
- let context = UIGraphicsGetCurrentContext()
- context?.setFillColor(color.cgColor)
- context?.fill(rect)
-
- guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return UIImage() }
- UIGraphicsEndImageContext()
-
- return image
- }
- }
|