mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 14:20:20 +00:00
Refactor PasswordSetupUI, PassportUI, GalleryUI and related modules
This commit is contained in:
48
submodules/ImageCompression/Sources/ImageCompression.swift
Normal file
48
submodules/ImageCompression/Sources/ImageCompression.swift
Normal file
@@ -0,0 +1,48 @@
|
||||
import Foundation
|
||||
import AVFoundation
|
||||
import UIKit
|
||||
|
||||
public func compressImageToJPEG(_ image: UIImage, quality: Float) -> Data? {
|
||||
let data = NSMutableData()
|
||||
guard let destination = CGImageDestinationCreateWithData(data as CFMutableData, "public.jpeg" as CFString, 1, nil) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let options = NSMutableDictionary()
|
||||
options.setObject(quality as NSNumber, forKey: kCGImageDestinationLossyCompressionQuality as NSString)
|
||||
|
||||
guard let cgImage = image.cgImage else {
|
||||
return nil
|
||||
}
|
||||
CGImageDestinationAddImage(destination, cgImage, options as CFDictionary)
|
||||
CGImageDestinationFinalize(destination)
|
||||
|
||||
if data.length == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return data as Data
|
||||
}
|
||||
|
||||
@available(iOSApplicationExtension 11.0, iOS 11.0, *)
|
||||
public func compressImage(_ image: UIImage, quality: Float) -> Data? {
|
||||
let data = NSMutableData()
|
||||
guard let destination = CGImageDestinationCreateWithData(data as CFMutableData, AVFileType.heic as CFString, 1, nil) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let options = NSMutableDictionary()
|
||||
options.setObject(quality as NSNumber, forKey: kCGImageDestinationLossyCompressionQuality as NSString)
|
||||
|
||||
guard let cgImage = image.cgImage else {
|
||||
return nil
|
||||
}
|
||||
CGImageDestinationAddImage(destination, cgImage, options as CFDictionary)
|
||||
CGImageDestinationFinalize(destination)
|
||||
|
||||
if data.length == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return data as Data
|
||||
}
|
||||
Reference in New Issue
Block a user