-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIImage+Additions.swift
More file actions
34 lines (25 loc) · 871 Bytes
/
UIImage+Additions.swift
File metadata and controls
34 lines (25 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// UIImage+Additions.swift
// RentAdate
//
// Created by Kassandra Capretta on 2/2/20.
// Copyright © 2020 Kassandra Capretta. All rights reserved.
//
import UIKit
extension UIImage {
var scaledToSafeUploadSize: UIImage? {
let maxImageSideLength: CGFloat = 480
let largerSide: CGFloat = max(size.width, size.height)
let ratioScale: CGFloat = largerSide > maxImageSideLength ? largerSide / maxImageSideLength : 1
let newImageSize = CGSize(width: size.width / ratioScale, height: size.height / ratioScale)
return image(scaledTo: newImageSize)
}
func image(scaledTo size: CGSize) -> UIImage? {
defer {
UIGraphicsEndImageContext()
}
UIGraphicsBeginImageContextWithOptions(size, true, 0)
draw(in: CGRect(origin: .zero, size: size))
return UIGraphicsGetImageFromCurrentImageContext()
}
}