import Foundation
import UIKit
extension 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
}
}
圖片壓縮處理
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.