WICompress
A lightweight iOS image compression library that supports JPEG, PNG, HEIC formats and uses the Luban algorithm for compression.
Install / Use
/learn @Weixi779/WICompressREADME
WICompress
English | 简体中文
WICompress is a lightweight iOS image compression library that supports JPEG, PNG, and HEIC formats, using the Luban algorithm for intelligent compression and efficient image processing.
Features
-
Luban Algorithm Compression - Calculates optimal compression ratios to reduce file size while maintaining quality
-
Multi-format Support - Supports JPEG, PNG, and HEIC formats with automatic format-specific compression methods
-
High Performance - Optimized processing using
UIImageandCGImageDestination -
iOS 14+ Compatible - Built with Swift Package Manager for modern iOS development
-
Swift 6 Ready - Migrated for Swift 6, including concurrency/sendability compatibility improvements
Installation
Swift Package Manager (SPM)
- Open your project in Xcode, select File → Add Packages
- Enter the repository URL: https://github.com/Weixi779/WICompress
- Select the latest version and click Add Package
Usage
Image Resizing
Resize images using the Luban algorithm, which only compresses resolution:
import WICompress
let resizedImage = WICompress.resizeImage(originalImage)
Image Quality Compression
Compress images with quality control and format preservation:
import WICompress
let compressedData = WICompress.compressImage(
originalImage,
quality: 0.7,
formatData: imageData
)
Parameters
image: TheUIImageto be compressedquality: Compression quality (0.0 - 1.0), default is 0.6formatData: Original image data used for format detection. If nil, defaults to JPEG compression
Important Notes:
- If
formatDatais nil, the library defaults to JPEG compression - For HEIC images, providing
formatDatais strongly recommended for optimal compression results
Processing Workflow
- Quality Compression: Input (
UIImage+Data) → Quality-compressedData - Resolution Adjustment: Quality-compressed
Data→UIImage→ ResizedUIImage(based on business requirements) - Format Conversion: Convert
UIImageto requiredDataformat for backend processing
Example Project
The repository includes a comprehensive SwiftUI example project demonstrating WICompress functionality:
Features Demonstrated
- PhotosPicker Integration: Select images from photo library with HEIC format preservation
- PHPickerViewController: UIKit-based picker for advanced use cases
- Real-time Comparison: Side-by-side comparison of original vs compressed images
- Format Detection: Automatic detection of JPEG, PNG, and HEIC formats
- Live Photo Support: Special handling for HEIC Live Photos
- Compression Metrics: File size reduction and compression ratio display
Running the Example
- Open
Example/WICompressExample/WICompressExample.xcodeproj - Build and run on iOS device or simulator
- Select images from your photo library to test compression
The example includes two main tabs:
- PhotosPicker: SwiftUI-based image selection
- PHPicker: UIKit-based image selection with enhanced debugging
Compression Results
| PNG Result | HEIC Result 1 | HEIC Result 2 | | --- | --- | --- | | <img src="https://github.com/user-attachments/assets/901baf3d-93c5-4637-b15b-667a0f87bb1d" width="200"> | <img src="https://github.com/user-attachments/assets/582add53-6550-446b-ab0b-f0785ffc3327" width="200"> | <img src="https://github.com/user-attachments/assets/a960de4e-94e8-473e-828f-bf2db03dd1c2" width="200"> |
License
WICompress is available under the MIT license. See the LICENSE file for more info.
