Releases: Little-tale/SwiftImageCompressor
Releases · Little-tale/SwiftImageCompressor
0.0.4
UPDATE
1. The "DownSample" function is converted to "Public".
2. Task.detached (operation: _) that was being used internally has been removed
This means that the task attribute is inherited and applied at the discretion of the developer using the function. When canceling the task, modify the logic so that the internal logic stops.
3. Additionally, we have added onlyCompress functions.
/// Compress the given image directly without resizing, targeting a specified file size in MB.
/// This function keeps the original image dimensions but compresses it to meet the desired size.
/// - Parameters:
/// - type: The desired image format (`jpeg` or `png`).
/// - targetMB: The maximum allowed file size in megabytes.
/// - Returns: The compressed image data (`Data?`), or `nil` if compression fails.
public func onlyCompressImage(type: ImageType, targetMB: Double) async -> Data? {
return await SwiftImageCompressor.shared.onlyCompressImage(self, type: type, targetMB: targetMB)
}
/// Compress the given image directly without resizing, targeting a specified file size in MB.
/// This function keeps the original image dimensions but compresses it to meet the desired size.
/// - Parameters:
/// - type: The desired image format (`jpeg` or `png`).
/// - targetMB: The maximum allowed file size in megabytes.
/// - completion: The compressed image data (`Data?`), or `nil` if compression fails.
public func onlyCompressImage(
type: ImageType,
targetMB: Double,
completion: @Sendable @escaping (Data?) -> Void
) {
SwiftImageCompressor.shared.onlyCompressImage(self, type: type, targetMB: targetMB, completion: completion)
}0.0.3
UPDATE LIST
- Fix the issue where part of the memory remains during image compression.
- Removed the
resizeImage(_ image, maxDimension:)function. - Image resizing is now performed internally using a new
downSample(_ image, maxDimension:)method for improved memory efficiency during compression. - The
downSamplemethod is currently internal and not yet public. - It will be revealed in the next update so that developers can make more control over the downsampling behavior.
@available(*, message: "This Function Will Be Deprecated Soon Use downsampling function")
public func resizeImage(_ image: UIImage, maxDimension: CGFloat) -> UIImage {
let width = image.size.width
let height = image.size.height
guard width > maxDimension || height > maxDimension else {
return image
}
let aspectRatio: CGFloat = width / height
var newSize: CGSize
// Vertical
if aspectRatio > 1 {
newSize = CGSize(width: maxDimension, height: maxDimension / aspectRatio)
}
// Horizontal
else {
newSize = CGSize(width: maxDimension * aspectRatio, height: maxDimension)
}
let renderer = UIGraphicsImageRenderer(size: newSize)
return renderer.image { context in
image.draw(in: CGRect(origin: .zero, size: newSize))
}
}0.0.2
0.0.2 / UPDATE LIST
- README
- UNIT TEST
- Package.swift Swift Tool Version 6.0 -> 5.9
0.0.1
0.0.1 Update
ImageCompress And Resizing