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 datalet imageData = UIImageJPEGRepresentation(image, quality)
ifletdata= 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 contextlet scaleImage = UIGraphicsGetImageFromCurrentImageContext()
//remove the current context from the top of the stack
UIGraphicsEndImageContext()
return scaleImage
}
}