123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import UIKit
- extension UIImage {
- func imageResize(sizeChange: CGSize) -> UIImage {
- let hasAlpha = true
- let scale: CGFloat = 0.0 // Use scale factor of main screen
- UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)
- draw(in: CGRect(origin: CGPoint.zero, size: sizeChange))
- let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
- return scaledImage!
- }
- public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
- let rect = CGRect(origin: .zero, size: size)
- UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
- color.setFill()
- UIRectFill(rect)
- let image = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- guard let cgImage = image?.cgImage else { return nil }
- self.init(cgImage: cgImage)
- }
- func resizeImage(targetSize: CGSize) -> UIImage? {
- let size = self.size
- let widthRatio = targetSize.width / size.width
- let heightRatio = targetSize.height / size.height
- var newSize: CGSize
- if widthRatio > heightRatio {
- newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
- } else {
- newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
- }
- let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
- UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
- draw(in: rect)
- let newImage = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- return newImage
- }
- func dcCompress(toMax target: Float = 1280) -> UIImage? {
- return resize(toMax: target)
- }
- func imageSizeInPixel() -> CGSize {
- let heightInPoints = size.height
- let heightInPixels = heightInPoints * scale
- let widthInPoints = size.width
- let widthInPixels = widthInPoints * scale
- return CGSize(width: widthInPixels, height: heightInPixels)
- }
- // source: https://stackoverflow.com/questions/29137488/how-do-i-resize-the-uiimage-to-reduce-upload-image-size // slightly changed
- func resize(toMax: Float) -> UIImage? {
- var actualHeight = Float(size.height)
- var actualWidth = Float(size.width)
- let maxHeight: Float = toMax
- let maxWidth: Float = toMax
- var imgRatio: Float = actualWidth / actualHeight
- let maxRatio: Float = maxWidth / maxHeight
- let compressionQuality: Float = 0.5
- //50 percent compression
- if actualHeight > maxHeight || actualWidth > maxWidth {
- if imgRatio < maxRatio {
- //adjust width according to maxHeight
- imgRatio = maxHeight / actualHeight
- actualWidth = imgRatio * actualWidth
- actualHeight = maxHeight
- } else if imgRatio > maxRatio {
- //adjust height according to maxWidth
- imgRatio = maxWidth / actualWidth
- actualHeight = imgRatio * actualHeight
- actualWidth = maxWidth
- } else {
- actualHeight = maxHeight
- actualWidth = maxWidth
- }
- }
- let rect = CGRect(x: 0.0, y: 0.0, width: CGFloat(actualWidth), height: CGFloat(actualHeight))
- UIGraphicsBeginImageContext(rect.size)
- draw(in: rect)
- let img = UIGraphicsGetImageFromCurrentImageContext()
- let imageData = img?.jpegData(compressionQuality: CGFloat(compressionQuality))
- UIGraphicsEndImageContext()
- return UIImage(data: imageData!)
- }
- public class func messageKitImageWith(type: ImageType) -> UIImage? {
- let assetBundle = Bundle.messageKitAssetBundle()
- let imagePath = assetBundle.path(forResource: type.rawValue, ofType: "png", inDirectory: "Images")
- let image = UIImage(contentsOfFile: imagePath ?? "")
- return image
- }
- }
- public enum ImageType: String {
- case play
- case pause
- case disclouser
- }
|