Skip to content

A lightweight iOS image compression library that supports JPEG, PNG, HEIC formats and uses the Luban algorithm for compression.

License

Notifications You must be signed in to change notification settings

Weixi779/WICompress

Repository files navigation

WICompress

Platform Swift SPM Support License

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 UIImage and CGImageDestination

  • iOS 14+ Compatible - Built with Swift Package Manager for modern iOS development

Installation

Swift Package Manager (SPM)

  1. Open your project in Xcode, select FileAdd Packages
  2. Enter the repository URL: https://github.com/Weixi779/WICompress
  3. 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: The UIImage to be compressed
  • quality: Compression quality (0.0 - 1.0), default is 0.6
  • formatData: Original image data used for format detection. If nil, defaults to JPEG compression

Important Notes:

  • If formatData is nil, the library defaults to JPEG compression
  • For HEIC images, providing formatData is strongly recommended for optimal compression results

Processing Workflow

  1. Quality Compression: Input (UIImage + Data) → Quality-compressed Data
  2. Resolution Adjustment: Quality-compressed DataUIImage → Resized UIImage (based on business requirements)
  3. Format Conversion: Convert UIImage to required Data format 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

  1. Open Example/WICompressExample/WICompressExample.xcodeproj
  2. Build and run on iOS device or simulator
  3. 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

License

WICompress is available under the MIT license. See the LICENSE file for more info.


If you find this project helpful, please give it a Star ⭐️!

About

A lightweight iOS image compression library that supports JPEG, PNG, HEIC formats and uses the Luban algorithm for compression.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages