Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct CompressionService {
- /**
- compress 'image' to 'maxSize' size
- - Parameter image: The image to compress.
- - Parameter maxSize: The maximum size in MB to image.
- - Returns: A Data image with 'maxSize' applied.
- */
- static func compress(image: UIImage, maxSize: CGFloat) -> Data? {
- guard let imageSize = UIImageJPEGRepresentation(image, 1)?.count else { return nil }
- let compressionRate: CGFloat = (maxSize * 1024) / CGFloat(imageSize)
- return UIImageJPEGRepresentation(image, compressionRate)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement